log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
%%%% Error Creating SessionFactory %%%%
org.hibernate.MappingException: persistent class not known: java.util.Set
at org.hibernate.cfg.Configuration$1.getIdentifierType(Configuration.java:2158)
at org.hibernate.type.EntityType.getIdentifierType(EntityType.java:520)
at org.hibernate.type.EntityType.getIdentifierOrUniqueKeyType(EntityType.java:545)
at org.hibernate.persister.entity.AbstractPropertyMapping.initIdentifierPropertyPaths(AbstractPropertyMapping.java:191)
at org.hibernate.persister.entity.AbstractPropertyMapping.initPropertyPaths(AbstractPropertyMapping.java:181)
at org.hibernate.persister.entity.AbstractEntityPersister.initOrdinaryPropertyPaths(AbstractEntityPersister.java:1725)
at org.hibernate.persister.entity.AbstractEntityPersister.initPropertyPaths(AbstractEntityPersister.java:1755)
at org.hibernate.persister.entity.AbstractEntityPersister.postConstruct(AbstractEntityPersister.java:2932)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:431)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:267)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at hibernate.utils.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:20)
at hibernate.test.junittest.setUpBeforeClass(junittest.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
1 package hibernate.Utility; 2 3 import java.io.Serializable; 4 import java.util.HashSet; 5 import java.util.Set; 6 7 import javax.persistence.CascadeType; 8 import javax.persistence.Column; 9 import javax.persistence.Entity; 10 import javax.persistence.FetchType; 11 import javax.persistence.GeneratedValue; 12 import javax.persistence.GenerationType; 13 import javax.persistence.Id; 14 import javax.persistence.OneToOne; 15 import javax.persistence.PrimaryKeyJoinColumn; 16 import javax.persistence.SequenceGenerator; 17 import javax.persistence.Table; 18 import javax.persistence.Version; 19 20 /** 21 * 用户注册和登录表 22 * @author Administrator 23 *(用户id,email&phone,pwd,userinfo) 24 *当前表与用户详细信息表一对一 25 */ 26 @Entity 27 @Table(name = "regandlogin") 28 public class regandlogin implements Serializable{ 29 30 private static final long serialVersionUID = 875363728572540306L; 31 32 private long uid; 33 private String emailandphone; 34 private String upwd; 35 private int usertype; 36 private long uinfoid; 37 private Set<tuserinfo> infoset = new HashSet<tuserinfo>(); 38 public regandlogin() { 39 super(); 40 // TODO Auto-generated constructor stub 41 } 42 public regandlogin(String emailandphone, String upwd, int usertype, 43 long uinfoid, Set<tuserinfo> infoset) { 44 super(); 45 this.emailandphone = emailandphone; 46 this.upwd = upwd; 47 this.usertype = usertype; 48 this.uinfoid = uinfoid; 49 this.infoset = infoset; 50 } 51 public regandlogin(long uid, String emailandphone, String upwd, 52 int usertype, long uinfoid, Set<tuserinfo> infoset) { 53 super(); 54 this.uid = uid; 55 this.emailandphone = emailandphone; 56 this.upwd = upwd; 57 this.usertype = usertype; 58 this.uinfoid = uinfoid; 59 this.infoset = infoset; 60 } 61 @Version 62 @Id 63 @SequenceGenerator(name = "Utilityseq_id") 64 @GeneratedValue(strategy = GenerationType.SEQUENCE ) 65 public long getUid() { 66 return uid; 67 } 68 public void setUid(long uid) { 69 this.uid = uid; 70 } 71 @Column(unique = true) 72 public String getEmailandphone() { 73 return emailandphone; 74 } 75 public void setEmailandphone(String emailandphone) { 76 this.emailandphone = emailandphone; 77 } 78 @Column 79 public String getUpwd() { 80 return upwd; 81 } 82 public void setUpwd(String upwd) { 83 this.upwd = upwd; 84 } 85 @Column(columnDefinition = "int default 1") 86 public int getUsertype() { 87 return usertype; 88 } 89 public void setUsertype(int usertype) { 90 this.usertype = usertype; 91 } 92 @Column(unique = true) 93 public long getUinfoid() { 94 return uinfoid; 95 } 96 public void setUinfoid(long uinfoid) { 97 this.uinfoid = uinfoid; 98 } 99 /** 100 * CascadeType.MERGE 级联更新 101 * CascadeType.REMOVE 级联删除 102 * 103 * @PrimaryKeyJoinColumn 这个注解只能写在主(生成ID)的一端 104 * @return 105 */ 106 @OneToOne(cascade = {CascadeType.MERGE,CascadeType.REMOVE},fetch = FetchType.LAZY) 107 @PrimaryKeyJoinColumn 108 public Set<tuserinfo> getInfoset() { 109 return infoset; 110 } 111 public void setInfoset(Set<tuserinfo> infoset) { 112 this.infoset = infoset; 113 } 114 @Override 115 public String toString() { 116 return "regandlogin [emailandphone=" + emailandphone + ", uid=" + uid 117 + ", uinfoid=" + uinfoid + ", upwd=" + upwd + ", usertype=" 118 + usertype + "]"; 119 } 120 121 } 122 ----------------------------------------------------------------------- 123 /** 124 * 公共bean包 125 */ 126 package hibernate.Utility; 127 128 import java.io.Serializable; 129 import java.util.Date; 130 import java.util.HashSet; 131 import java.util.Set; 132 133 import javax.persistence.CascadeType; 134 import javax.persistence.Column; 135 import javax.persistence.Entity; 136 import javax.persistence.GeneratedValue; 137 import javax.persistence.GenerationType; 138 import javax.persistence.Id; 139 import javax.persistence.JoinTable; 140 import javax.persistence.ManyToMany; 141 import javax.persistence.OneToOne; 142 import javax.persistence.PrimaryKeyJoinColumn; 143 import javax.persistence.SequenceGenerator; 144 import javax.persistence.Table; 145 import javax.persistence.Version; 146 147 /** 148 * 用户表 149 * @author Administrator 150 * (用户编号,用户真实姓名,用户性别,用户年龄,用户所属部门表,用户登录注册表) 151 */ 152 @Entity 153 @Table(name = "tuserinfo") 154 public class tuserinfo implements Serializable{ 155 156 private static final long serialVersionUID = 3710315229900807036L; 157 158 private long uinfoid; 159 private String realname; 160 private String sex; 161 private Date birthday; 162 private long uid; 163 private long deptid; 164 private Set<regandlogin> regandloginset = new HashSet<regandlogin>(); 165 private Set<dept> deptset = new HashSet<dept>(); 166 public tuserinfo() {super();} 167 168 public tuserinfo(String realname, String sex, Date birthday, long uid, 169 long deptid, Set<regandlogin> regandloginset, Set<dept> deptset) { 170 super(); 171 this.realname = realname; 172 this.sex = sex; 173 this.birthday = birthday; 174 this.uid = uid; 175 this.deptid = deptid; 176 this.regandloginset = regandloginset; 177 this.deptset = deptset; 178 } 179 180 public tuserinfo(long uinfoid, String realname, String sex, Date birthday, 181 long uid, long deptid, Set<regandlogin> regandloginset, 182 Set<dept> deptset) { 183 super(); 184 this.uinfoid = uinfoid; 185 this.realname = realname; 186 this.sex = sex; 187 this.birthday = birthday; 188 this.uid = uid; 189 this.deptid = deptid; 190 this.regandloginset = regandloginset; 191 this.deptset = deptset; 192 } 193 @Version 194 @Id 195 @SequenceGenerator(sequenceName = "userinfo_seq_id", name = "userinfo_seq_id") 196 @GeneratedValue(strategy = GenerationType.SEQUENCE) 197 public long getUinfoid() { 198 return uinfoid; 199 } 200 public void setUinfoid(long uinfoid) { 201 this.uinfoid = uinfoid; 202 } 203 @Column 204 public String getRealname() { 205 return realname; 206 } 207 public void setRealname(String realname) { 208 this.realname = realname; 209 } 210 @Column 211 public String getSex() { 212 return sex; 213 } 214 public void setSex(String sex) { 215 this.sex = sex; 216 } 217 @Column 218 public Date getBirthday() { 219 return birthday; 220 } 221 public void setBirthday(Date birthday) { 222 this.birthday = birthday; 223 } 224 @Column(unique = true) 225 public long getUid() { 226 return uid; 227 } 228 229 public void setUid(long uid) { 230 this.uid = uid; 231 } 232 233 @OneToOne(cascade = {CascadeType.MERGE,CascadeType.REMOVE}) 234 @PrimaryKeyJoinColumn 235 public Set<regandlogin> getRegandloginset() { 236 return regandloginset; 237 } 238 239 public void setRegandloginset(Set<regandlogin> regandloginset) { 240 this.regandloginset = regandloginset; 241 } 242 243 @Column(unique = true) 244 public long getDeptid() { 245 return deptid; 246 } 247 public void setDeptid(long deptid) { 248 this.deptid = deptid; 249 } 250 @ManyToMany(cascade = {CascadeType.MERGE,CascadeType.REMOVE}) 251 @JoinTable(name = "infodept") 252 public Set<dept> getDeptset() { 253 return deptset; 254 } 255 public void setDeptset(Set<dept> deptset) { 256 this.deptset = deptset; 257 } 258 259 @Override 260 public String toString() { 261 return "tuserinfo [birthday=" + birthday + ", deptid=" + deptid 262 + ", realname=" + realname + ", sex=" + sex + ", uid=" + uid 263 + ", uinfoid=" + uinfoid + "]"; 264 } 265 266 } 267 --------------------------------------------------------------------------- 268 package hibernate.Utility; 269 270 import java.io.Serializable; 271 import java.util.HashSet; 272 import java.util.Set; 273 274 import javax.persistence.CascadeType; 275 import javax.persistence.Column; 276 import javax.persistence.Entity; 277 import javax.persistence.GeneratedValue; 278 import javax.persistence.GenerationType; 279 import javax.persistence.Id; 280 import javax.persistence.JoinTable; 281 import javax.persistence.ManyToMany; 282 import javax.persistence.SequenceGenerator; 283 import javax.persistence.Table; 284 import javax.persistence.Version; 285 286 /** 287 * 用户部门表 288 * @author 289 *(部门id,部门名称,用户id) 290 */ 291 292 @Entity 293 @Table(name = "dept") 294 public class dept implements Serializable { 295 296 private static final long serialVersionUID = -329715089648931324L; 297 298 private long deptid; 299 private String deptname; 300 private long uinfoid; 301 private Set<tuserinfo> uinfoset = new HashSet<tuserinfo>(); 302 public dept() {super();} 303 @Version 304 @Id 305 @SequenceGenerator(name = "dept_seq_id", sequenceName = "dept_seq_id" ) 306 @GeneratedValue(strategy = GenerationType.SEQUENCE) 307 public long getDeptid() { 308 return deptid; 309 } 310 public void setDeptid(long deptid) { 311 this.deptid = deptid; 312 } 313 @Column(unique = true) 314 public String getDeptname() { 315 return deptname; 316 } 317 public void setDeptname(String deptname) { 318 this.deptname = deptname; 319 } 320 @Column 321 public long getUinfoid() { 322 return uinfoid; 323 } 324 public void setUinfoid(long uinfoid) { 325 this.uinfoid = uinfoid; 326 } 327 /** 328 * cascade = {CascadeType.MERGE,CascadeType.REMOVE} 329 * @return 330 */ 331 @ManyToMany(cascade = CascadeType.ALL) 332 @JoinTable(name = "infodept") 333 public Set<tuserinfo> getUinfoset() { 334 return uinfoset; 335 } 336 public void setUinfoset(Set<tuserinfo> uinfoset) { 337 this.uinfoset = uinfoset; 338 } 339 @Override 340 public String toString() { 341 return "dept [deptid=" + deptid + ", deptname=" + deptname 342 + ", uinfoid=" + uinfoid + "]"; 343 } 344 345 346 }
1 <?xml version='1.0' encoding='UTF-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 6 <!-- Generated by MyEclipse Hibernate Tools. --> 7 <hibernate-configuration> 8 9 <session-factory> 10 <property name="dialect"> 11 org.hibernate.dialect.Oracle9Dialect 12 </property> 13 <property name="connection.url"> 14 jdbc:oracle:thin:@127.0.0.1:1521:orcl 15 </property> 16 <property name="connection.username">yingheart</property> 17 <property name="connection.password">197182208</property> 18 <property name="connection.driver_class"> 19 oracle.jdbc.driver.OracleDriver 20 </property> 21 <property name="myeclipse.connection.profile">yingheart</property> 22 23 <property name="hibernate.hbm2ddl.auto">update</property> 24 <property name="show_sql">true</property> 25 <property name="format_sql">true</property> 26 27 <mapping class="hibernate.Utility.dept" /> 28 <mapping class="hibernate.Utility.regandlogin" /> 29 <mapping class="hibernate.Utility.tuserinfo" /> 30 31 </session-factory> 32 33 </hibernate-configuration>
1 package hibernate.test; 2 3 import hibernate.Utility.dept; 4 import hibernate.utils.HibernateSessionFactory; 5 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.Transaction; 9 import org.junit.AfterClass; 10 import org.junit.BeforeClass; 11 import org.junit.Test; 12 13 public class junittest { 14 15 private static SessionFactory sf; 16 private static Session ss; 17 18 @BeforeClass 19 public static void setUpBeforeClass() throws Exception { 20 sf = HibernateSessionFactory.getSessionFactory(); 21 //sf = Hibernate.getSessionFactory(); 22 ss = sf.openSession(); 23 } 24 25 @AfterClass 26 public static void tearDownAfterClass() throws Exception { 27 if (ss != null) { 28 ss.close(); 29 } 30 if (sf != null) { 31 sf.close(); 32 } 33 } 34 35 @Test 36 public void add(){ 37 Transaction tra = ss.beginTransaction(); 38 39 dept d = new dept(); 40 d.setDeptid(1); 41 d.setDeptname("dfhdg"); 42 d.setUinfoid(0); 43 ss.save(d); 44 tra.commit(); 45 } 46 47 //public static void main(String[] args) { 48 // HibernateUtil.getConfiguration(); 49 //} 50 51 }