码迷,mamicode.com
首页 > Web开发 > 详细

hibernate4.3.10使用注解映射方式样例

时间:2015-08-04 13:05:49      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

1.调用例子

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;


public static IDCard insertIDCard(){

Configuration cfg = new Configuration().configure();


StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(cfg.getProperties()).build();

SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);
Session session = sf.openSession();

session.beginTransaction();


Person p = new Person();
p.setName("xiaoluo");
session.save(p);

IDCard card = new IDCard();
card.setNo("1111111111");
card.setPerson(p);
session.save(card);

session.getTransaction().commit();
session.close();
sf.close();

return card;


}

2. 主配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"D:\j2ee_workspace\test\config\hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.password">td2000</property>
<property name="hibernate.connection.url">jdbc:sqlserver://testserver:1433;databaseName=plusoft_test</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2012Dialect</property>
<property name="show_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="plusoft_test.IDCard"/>
<mapping class="plusoft_test.Person"/>
</session-factory>
</hibernate-configuration>

 

3.实体类注解

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="t_person")
public class Person
{
private int id;
private String name;
private IDCard card;

@OneToOne(mappedBy="person")
public IDCard getCard()
{
return card;
}
public void setCard(IDCard card)
{
this.card = card;
}
@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;
}

}

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;


@Entity
@Table(name="t_id_card")
public class IDCard
{
private int id;
private String no;
private Person person;

@Id
@GeneratedValue
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getNo()
{
return no;
}
public void setNo(String no)
{
this.no = no;
}
@OneToOne
@JoinColumn(name="pid")
public Person getPerson()
{
return person;
}
public void setPerson(Person person)
{
this.person = person;
}
}

hibernate4.3.10使用注解映射方式样例

标签:

原文地址:http://www.cnblogs.com/coolyylu/p/4701557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!