1)基于
Annotation:
多方User:
package com.stark.
hibernate.model;
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 String id;
private String name;
private Group group;
/**
*
* 只需在多方User中添加一个Group同时添加
注解@ManyToOne即可(Many代表User,One代表Group)
*/
@ManyToOne
@JoinColumn(name="groupId")
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
@Id
//@GeneratedValue
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
一方:
package com.stark.hibernate.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_group")
public class Group {
private String id;
private String name;
@Id
//@GeneratedValue
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
利用一下代码
public class Test
SchemaExport {
@Test
public void testSchemaExport() {
new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
}
}
将会生成如下DDL语句:
create table t_group (groupId varchar(255) not null, groupName varchar(255), primary key (groupId))
create table t_user (userId varchar(255) not null, userName varchar(255), groupId varchar(255), primary key (userId))
alter table t_user add index FKCB63CCB65F00FCD6 (groupId), add constraint FKCB63CCB65F00FCD6 foreign key (groupId) references t_group (groupId)
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.stark.hibernate.model.Group;
import com.stark.hibernate.model.User;
import com.stark.hibernate.util.HibernateUtil;
import com.stark.hibernate.util.Hibernate_AnnotationUtil;
public class UserTest {
private static SessionFactory sessionFactory;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
sessionFactory=Hibernate_AnnotationUtil.getSessionFactory();
}
@Test
public void saveUser() {
Session session = sessionFactory.openSession();
session.beginTransaction();
Group g =new Group();
g.setGroupId(01);
g.setGroupName("software");
// 如果不添加session.save(g),则不能成功保存,抛出TransientObjectException
异常
//因为Group为Transient状态,groupId没有分配值
//persistent状态的对象是不能引用transient状态的对象的
//采用cascade属性是解决TransientObjectException异常的一种手段
session.save(g);
User u= new User();
u.setUserId(01);
u.setUserName("ZhangSan");
u.setGroup(g);
session.save(u);
session.getTransaction().commit();
session.close();
}
@Test
public void getUser(){
Session session = sessionFactory.openSession();
session.beginTransaction();
User u=(User)session.get(User.class, 1);
System.out.println("UserId:"+u.getUserId());
System.out.println("UserName:"+u.getUserName());
System.out.println("GroupId:"+u.getGroup ().getGroupId());
System.out.println("GroupName:"+u.getGroup().getGroupName());
session.getTransaction().commit();
session.close();
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
sessionFactory.close();
}
}
查询结果如下:
UserId:1
UserName:ZhangSan
GroupId:1
GroupName:software
2)基于
XML:
将以上User和Group 中所有@注解去掉
Group.hbm.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.stark.hibernate.model">
<class name="Group" table="t_group">
<id name="groupId" >
<generator class="native"/>
</id>
<property name="groupName"/>
</class>
</hibernate-mapping>
User.hbm.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.stark.hibernate.model">
<class name="User" table="t_user">
<id name="userId">
<generator class="native"/>
</id>
<property name="userName" />
<
many-to-one name="group" column="groupId" ></many-to-one>
</class>
</hibernate-mapping>
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.stark.hibernate.model.Group;
import com.stark.hibernate.model.User;
import com.stark.hibernate.util.HibernateUtil;
import com.stark.hibernate.util.Hibernate_AnnotationUtil;
public class UserTest {
private static SessionFactory sessionFactory;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//sessionFactory=Hibernate_AnnotationUtil.getSessionFactory();
sessionFactory=HibernateUtil.getSessionFactory();
}
@Test
public void saveUser() {
Session session = sessionFactory.openSession();
session.beginTransaction();
Group g =new Group();
g.setGroupId(01);
g.setGroupName("software");
// 如果不添加session.save(g),则不能成功保存,抛出TransientObjectException异常
//因为Group为Transient状态,groupId没有分配值
//persistent状态的对象是不能引用transient状态的对象的
session.save(g);
User u= new User();
u.setUserId(01);
u.setUserName("ZhangSan");
u.setGroup(g);
session.save(u);
session.getTransaction().commit();
session.close();
}
@Test
public void getUser(){
Session session = sessionFactory.openSession();
session.beginTransaction();
User u=(User)session.get(User.class, 1);
System.out.println("UserId:"+u.getUserId());
System.out.println("UserName:"+u.getUserName());
System.out.println("GroupId:"+u.getGroup().getGroupId());
System.out.println("GroupName:"+u.getGroup().getGroupName());
session.getTransaction().commit();
session.close();
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
sessionFactory.close();
}
}
如在User.hbm.xml 中
添加:
<many-to-one name="group" column="groupId" cascade="all" ></many-to-one>
则在saveUser时就不用先saveGroup了,[size=xx-small][/size][align=center][/align]因为采用了cascade属性,所以它会先保存Group