|
先建数据库:
use test;
create table person
(
id int primary key AUTO_INCREMENT,
username varchar(20) not null,
password varchar(20) not null
);
create table mail
(
id int primary KEY AUTO_INCREMENT,
email varchar(50) not null,
pid int null
);
select * from person;
select * from mail;
Person表和Mail表不存在物理方面的OneToMany关系,这
种关系是通过Hibernate来维护的.
hibernate.cgf.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">true</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.url">
jdbc:mysql://localhost/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="transaction.flush_before_completion">true</property>
<mapping class="com.vo.Person" />
<mapping class="com.vo.Mail" />
</session-factory>
</hibernate-configuration>
Person.java:
package com.vo;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@SuppressWarnings("serial")
@Entity
@Table(name = "person")
public class Person implements Serializable
{
private Integer id;
private String username;
private String password;
private Set<Mail> mails = new HashSet<Mail>();
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY) //映射为单一对多关系
//@Basic(fetch=FetchType.LAZY) //和上面的fecth一样的效果,都是延时初始
@JoinColumn(name="pid") //加入要映射的列(外键列)
public Set<Mail> getMails()
{
return mails;
}
public void setMails(Set<Mail> mails)
{
this.mails = mails;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getUsername()
{
return username;
}
@Column(name="username",nullable=false)
public void setUsername(String username)
{
this.username = username;
}
@Column(name="password",nullable=false)
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
Mail.java:
package com.vo;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@SuppressWarnings("serial")
@Entity
@Table(name = "mail")
public class Mail implements Serializable
{
private Integer id;
private String email;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
@Column(name="email",nullable=false)
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
}
测试类:
package com.test;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.vo.Mail;
import com.vo.Person;
public class PersonTest
{
private Session session;
private Transaction transaction;
@Before
public void before()
{
session = new AnnotationConfiguration().configure()
.buildSessionFactory().openSession();
transaction = session.getTransaction();
}
@After
public void after()
{
session.close();
}
@Test
public void save()
{
transaction.begin();
Person person = new Person();
person.setUsername("zdw");
person.setPassword("admin");
Mail m1 = new Mail();
m1.setEmail("a@live.com");
Mail m2 = new Mail();
m2.setEmail("b@live.com");
person.getMails().add(m1);
person.getMails().add(m2);
session.save(person);
transaction.commit();
}
@Test
public void findById()
{
Person person = (Person) session.load(Person.class, 1);
Set<Mail> mails = person.getMails();
if(mails.size() > 0)
{
for(Iterator<Mail> i = mails.iterator(); i .hasNext();)
{
Mail m = i.next();
System.out.println(m.getEmail());
}
}
}
@Test
public void delete()
{
transaction.begin();
Person person = (Person) session.load(Person.class, 1);
session.delete(person);
transaction.commit();
}
}
源码可在我的网盘下载. 点此下载
|