说明:
一、例子是一个组有许多用户,但一个用户只能属于一个组
二、本次使用的Hibernate版本是hibernate-release-4.3.11.Final.
先写Annotation版本:
实体类:
Group.java:
package com.buaa.hibernate.bean;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
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(mappedBy="group")
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;
}
}
User.java:
package com.buaa.hibernate.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="t_user")
public class User {
private int id;
private String name;
private Group group;
@ManyToOne
@JoinColumn(name="groupId") //多的一方加外键
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
@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版本:
User.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.buaa.hibernate.bean">
<class name="User" table="t_user">
<id name="id">
<generator class="native" />
</id>
<property name="name"/>
<many-to-one name="group" column="groupId"></many-to-one>
</class>
<class name="Group" table="t_group">
<id name="id">
<generator class="native" />
</id>
<property name="name"/>
<set name="users">
<key column="groupId"></key>
<one-to-many class="User" />
</set>
</class>
</hibernate-mapping>
此处将两个类写在一个配置文件里面了.
下面是Hibernate配置文件:hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- Assume test is the database name -->
<property name="connection.url">jdbc:mysql://localhost/test</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- <property name="hbm2ddl.auto">create</property> -->
<!--
-->
<mapping resource="com/buaa/hibernate/bean/User.hbm.xml"/>
<!--
<mapping class="com.buaa.hibernate.bean.Group"/>
<mapping class="com.buaa.hibernate.bean.User"/>
-->
</session-factory>
</hibernate-configuration>
最后写Junit测试类:
MyTest.java:
package com.buaa.hibernate.bean;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class MyTest {
@Test
public void test(){
User user1 = new User();
user1.setName("u1");
User user2 = new User();
user2.setName("u2");
Set<User> users = new HashSet<User>();
users.add(user1);
users.add(user2);
Group group = new Group();
group.setName("A");
group.setUsers(users);
user1.setGroup(group);
user2.setGroup(group);
Configuration configuration = new Configuration().configure();
ServiceRegistry service = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
SessionFactory factory = configuration.buildSessionFactory(service);
Session session = factory.openSession();
session.beginTransaction();
session.save(user1);
session.save(user2);
session.save(group);
session.getTransaction().commit();
session.close();
factory.close();
}
@Test
public void SchemaExportTest(){
Configuration cfg = new Configuration().configure();
//第一个参数是否将sql语句输出到控制台,第二个参数是否在数据库中生成对应的表格
new SchemaExport(cfg).create(true,true);
}
}