Hibernate运行报错!
package mtm_bfk; import java.util.Date; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; /*建实体类*/ /*学生实体类*/ @Entity public class Students {//多方 private int sid;//学号 private String sname;//姓名 private String gender;//性别 private Date birthday;//出生日期 private String major;//专业 private Set<Teachers> teacher;//学生持有教师的集合 public Students() {//公有的默认的不带参数的构造方法 super(); } public Students(String sname, String gender, Date birthday, String major) {//带参数的构造方法 super(); this.sname=sname; this.gender = gender; this.birthday = birthday; this.major = major; } //私有属性进行封装 @Id @GeneratedValue public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } @ManyToMany @JoinTable( name="teachers_students",//中间表的名字 joinColumns={@JoinColumn(name="sid")},//反转控制字段的名字 inverseJoinColumns={@JoinColumn(name="tid")}//本身的外键关联关系的字段名称 ) public Set<Teachers> getTeacher() { return teacher; } public void setTeacher(Set<Teachers> teacher) { this.teacher = teacher; } }
教师类;
package mtm_bfk; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToMany; import org.hibernate.annotations.GenericGenerator; /*教师实体类*/ //被映射成一张表 @Entity//实体类、类注解 public class Teachers { @Id//指定主键属性 @GeneratedValue(generator="tid")//指定主键生成器、生成器的名字 @GenericGenerator(name="tid",strategy="assigned")//对应的@GenericGenerator、名字、生成策略(手工赋值) @Column(length=4)//指定字段长度的大小 private String tid;//教师的编号 private String tname;//教师的姓名 @ManyToMany(mappedBy="teachers") private Set<Students> stus;//教师持有学生的集合 public Teachers() {//不带参数的公有的构造方法 super(); } public Teachers(String tid, String tname) {//带参数的构造方法 super(); this.tid = tid; this.tname = tname; } //字段封装 public String getTid() { return tid; } public void setTid(String tid) { this.tid = tid; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } }
控制台:
五月 20, 2016 8:39:21 下午 org.hibernate.annotations.common.Version <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final} 五月 20, 2016 8:39:21 下午 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.2.21.Final} 五月 20, 2016 8:39:21 下午 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 五月 20, 2016 8:39:21 下午 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 五月 20, 2016 8:39:21 下午 org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 五月 20, 2016 8:39:21 下午 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 五月 20, 2016 8:39:21 下午 org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null 五月 20, 2016 8:39:21 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!) 五月 20, 2016 8:39:21 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 20 五月 20, 2016 8:39:21 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000006: Autocommit mode: false 五月 20, 2016 8:39:21 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/imooc_hibernate3?useUnicode=true&characterEncoding=utf8] 五月 20, 2016 8:39:21 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000046: Connection properties: {user=root, password=****} 五月 20, 2016 8:39:21 下午 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect