老师和学生:
Annotation配置:
package com.zchen.hibernate.manytmany;
import java.util.HashSet;
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;
import javax.persistence.Table;
@Entity
@Table(name="T_TEACHER")
public class Teacher {
private int id;
private String name;
private Set<Student> students = new HashSet<Student>();
@ManyToMany
@JoinTable(name="T_S",
joinColumns={@JoinColumn(name="TEACHER_ID")},
inverseJoinColumns={@JoinColumn(name="STUDENT_ID")}
)
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.zchen.hibernate.manytmany;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="T_STUDENT")
public class Student {
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Xml配置:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zchen.hibernate.manytmany">
<class name="Teacher" table="T_TEACHER">
<id name="id" column="ID">
<generator class="native">
</generator>
</id>
<property name="name" column="NAME"/>
<set name="Students" table="T_S">
<key column="TEACHER_ID"></key>
<many-to-many class="com.zchen.hibernate.manytmany.Student" column="STUDENT_ID"/>
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.zchen.hibernate.manytmany">
<class name="Student" table="T_STUDENT">
<id name="id" column="ID">
<generator class="native">
</generator>
</id>
<property name="name" column="NAME"/>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///db_czbk_hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<!-- <mapping class="com.zchen.hibernate.manytmany.Teacher"/>
<mapping class="com.zchen.hibernate.manytmany.Student"/> -->
<mapping resource="com/zchen/hibernate/manytmany/Teacher.hbm.xml" />
<mapping resource="com/zchen/hibernate/manytmany/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
package com.zchen.hibernate;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class Snippet {
@Test
public void testSchemaExport(){
new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
}
}
1596

被折叠的 条评论
为什么被折叠?



