Hibernate4.3.5 双向一对多示例
来源:互联网 发布:网络语言黑洞什么意思 编辑:程序博客网 时间:2024/06/05 12:07
源码下载地址
public class Group{
private long id;private String name;
private Set<User> userSet=new HashSet<User>();;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<User> getUserSet() {
return userSet;
}
public void setUserSet(Set<User> userSet) {
this.userSet = userSet;
}
public String toString(){
return "id:"+this.id+" \t name:"+this.name;
}
}
public class User{
private long id;
private String name;
private Group group;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public String toString(){
return "id:"+this.id+"\tname:"+this.name;
}
} <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="entity.Group" table="zhtt0_group">
<id name="id" type="long">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true" />
</property>
<set name="userSet"
table="zhtt0_user"
lazy="false"
cascade="save-update"
inverse="false">
<key column="groupId"/>
<one-to-many class="entity.User"/>
</set>
</class>
</hibernate-mapping> <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="entity.User" table="zhtt0_user">
<id name="id" type="long">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true" />
</property>
<many-to-one name="group"
class="entity.Group"
column="groupId"
cascade="save-update"
lazy="false"/>
</class>
</hibernate-mapping> public class UserTest01{
@Test
public void test01(){
Session session=HibernateUtil.getSessionFactory().openSession();
Transaction t=session.beginTransaction();
User u=new User();
u.setName("user"+(Math.random()+"").substring(0,10));
Group g=new Group();
g.setName("user"+(Math.random()+"").substring(0,10));
g.getUserSet().add(u);
session.save(g);
t.commit();
session.close();
}
} public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// 载入设置
Configuration configuration = new Configuration().configure();
// 创建serviceRegistry
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
// 获取sessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Throwable ex) {
System.err.println("InitialSessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
} <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- JDBC连接设置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="hbm2ddl.auto">update</property>
<!-- 连接池配置,使用c3p0连接池 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!-- 数据库语言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- sql设置 -->
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<!-- 持久类设置所有需要持久的类要在这里标明,以下是针对annotation注释的类 -->
<mapping resource="entity/group.hbm.xml" />
<mapping resource="entity/user.hbm.xml" />
</session-factory>
</hibernate-configuration>
1 0
- Hibernate4.3.5 双向一对多示例
- hibernate4- 双向一对多的关联映射
- Hibernate4自学入门(五)——双向一对多、inverse属性、级联删除、一对多双向自身映射
- jpa之hibernate4.2.4之双向关联之一对一、一对多、多对多
- Hibernate4一对多关系映射
- HibernateLab--双向一对多
- Hibernate一对多(双向)
- 双向一对多
- JPA---双向一对多
- Hibernate一对多(双向)
- 双向一对多映射
- Hibernate一对多(双向)
- 一对多双向关联
- 双向一对多映射
- 一对多双向关联
- 单向一对多和双向一对多
- Hibernate 双向一对一&双向一对多
- Hibernate4-5 映射一对多关联关系
- 黑马程序员——异常,String类,StringBuffer
- cocos2d-x-lua工程的lua脚本加密
- 实时错误‘364’ 对象已卸载
- VMware下Linux以NAT方式上网
- 改变从github开始吧
- Hibernate4.3.5 双向一对多示例
- 花心雨的世界
- Spring实例化Bean的三种方式
- redis数据结构
- 1058. A+B in Hogwarts (20)
- 关于strtok的一点简单认识
- Codeforces 453A Little Pony and Expected Maximum 概率期望
- Linux Shortcut Key
- [DFS][toj1009Sticks]