组和用户
一个组有多个用户,一个用户只能属于一个组:
表结构:在用户表中有组的外键
Annotation生成方式:
package com.zchen.hibernate.onetmany; 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.OneToMany; import javax.persistence.Table; @Entity @Table(name="T_GROUP") public class Group { private int id; private String name; private Set<User> users = new HashSet<User>(); @OneToMany @JoinColumn(name="GROUP_ID") public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } @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.onetmany; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="T_USER") public class User { 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.onetmany"> <class name="Group" table="T_GROUP"> <id name="id" column="ID"> <generator class="native"> </generator> </id> <property name="name" column="NAME"/> <set name="users"> <key column="GROUP_ID"></key> <one-to-many class="com.zchen.hibernate.onetmany.User"/> </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.onetmany"> <class name="User" table="T_USER"> <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.onetmany.Group" /> <mapping class="com.zchen.hibernate.onetmany.User" /> --> <mapping resource="com/zchen/hibernate/onetmany/Group.hbm.xml" /> <mapping resource="com/zchen/hibernate/onetmany/User.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);
}
}