标签:多对一 hibernate5 双向关联
package com.mycompany.demo.bean; import java.util.Set; public class Forum { private int fid; private String name; private Set<ForumPost> forumPosts; public int getFid() { return fid; } public void setFid(int fid) { this.fid = fid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<ForumPost> getForumPosts() { return forumPosts; } public void setForumPosts(Set<ForumPost> forumPosts) { this.forumPosts = forumPosts; } }
<?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"> <!-- package:指定<class/>所在的包 --> <hibernate-mapping package="com.mycompany.demo.bean"> <!-- name:类名 table:表名 catalog:数据库名称,默认为hibernate.cfg.xml中配置的数据库名称 --> <class name="Forum" table="forum" lazy="true"> <meta attribute="class-description"> This class contains the forum detail. </meta> <!-- name:属性名 colum:列名 --> <id name="fid" type="int" column="fid"> <!-- increment:hibernate维护主键值 identity:数据库自增长 sequence:序列 native:根据不同的数据库选择生成策略 uuid:通过UUID算法生成,实际使用较多 assigned:手工设置 --> <generator class="native"/> </id> <!-- length:字节长度 type:字段类型,支持java和hibernate类型 not-null:非空约束 unique:唯一性约束 --> <property name="name" column="name" /> <!-- lazy: false:直接加载 true:延迟加载 extra:特别延迟加载 fetch join:迫切左外连接 select:普通select查询 subselect :子查询方式 --> <set name="forumPosts"> <key column="fid"/> <one-to-many class="ForumPost"/> </set> </class> </hibernate-mapping>
package com.mycompany.demo.bean; public class ForumPost { private int pid; private String subject; private Forum forum; public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public Forum getForum() { return forum; } public void setForum(Forum forum) { this.forum = forum; } }
<?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"> <!-- package:指定<class/>所在的包 --> <hibernate-mapping package="com.mycompany.demo.bean"> <!-- name:类名 table:表名 catalog:数据库名称,默认为hibernate.cfg.xml中配置的数据库名称 --> <class name="ForumPost" table="forumpost"> <meta attribute="class-description"> This class contains the forumpost detail. </meta> <!-- name:属性名 colum:列名 --> <id name="pid" type="int" column="pid"> <!-- increment:hibernate维护主键值 identity:数据库自增长 sequence:序列 native:根据不同的数据库选择生成策略 uuid:通过UUID算法生成,实际使用较多 assigned:手工设置 --> <generator class="native"/> </id> <!-- length:字节长度 type:字段类型,支持java和hibernate类型 not-null:非空约束 unique:唯一性约束 --> <property name="subject" column="subject" type="string" length="50" not-null="true" unique="false"/> <!-- name:关联属性 column:关联属性在数据库对应的字段 class:关联属性所对应的类型 --> <many-to-one name="forum" class="Forum" column="fid" cascade="save-update" fetch="select" lazy="proxy"/> </class> <query name="queryAll">from Forum f left outer join fetch f.forumPosts</query> </hibernate-mapping>
package com.mycompany.demo.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HbnUtil { private static SessionFactory sessionFactory; public static Session getSession(){ if(sessionFactory == null || sessionFactory.isClosed()){ sessionFactory = new Configuration().configure().buildSessionFactory(); } return sessionFactory.getCurrentSession(); } }
<?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> <!-- 方言,可以从Hibernate核心jar(hibernate-core-x.x.x.Finall.jar) 文件中的or.hibernate.dialect包中找到相应的类,类的全名就是 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 驱动 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库连接地址 --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> <!-- 用户名 --> <property name="hibernate.connection.username"> root </property> <!-- 密码 --> <property name="hibernate.connection.password"></property> <!-- create:每次都新创建,如果存在就删除 create-drop:创建新表,sessionFactory关闭,表会删除 update :表字段增加,会同步,字段减少不同步,数据改变会同步修改 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 输出sql --> <property name="hibernate.show_sql">true</property> <!-- 格式化sql --> <property name="hibernate.format_sql">true</property> <!-- 事务环境一个线程对一个事务 thread:本地事务环境 jta:分布式事务环境 SpringSessionContext:用于ssh整合 --> <property name="hibernate.current_session_context_class">thread</property> <!-- 使用c3p0数据源 --> <property name="hibernate.connection.provider_class"> org.hibernate.c3p0.internal.C3P0ConnectionProvider</property> <!-- List of XML mapping files --> <mapping resource="com/mycompany/demo/bean/Forum.hbm.xml"/> <mapping resource="com/mycompany/demo/bean/ForumPost.hbm.xml"/> </session-factory> </hibernate-configuration>
package com.mycompany.demo.bean; import java.util.HashSet; import java.util.List; import java.util.Set; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.sql.JoinType; import org.junit.Before; import org.junit.Test; import com.mycompany.demo.util.HbnUtil; public class TestApp { private Session session; @Before public void init(){ session = HbnUtil.getSession(); } /* * 一对多双向关联-添加 * 需要设置Forum.hbm.xml中的Set属性为cascade="all" */ @Test public void testOneToManyAdd(){ try { session.beginTransaction(); ForumPost forumPost1 = new ForumPost(); forumPost1.setSubject("A"); ForumPost forumPost2 = new ForumPost(); forumPost2.setSubject("B"); Set<ForumPost> forumPosts = new HashSet<ForumPost>(); forumPosts.add(forumPost1); forumPosts.add(forumPost2); Forum forum = new Forum(); forum.setName("foruma"); forum.setForumPosts(forumPosts); session.save(forum); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } } /* * 多对一双向关联-fetch="select",lazy="proxy",在一的一方的class标签中添加lazy="true"属性 */ @Test public void testFetchForSelect(){ try { session.beginTransaction(); ForumPost forumPost = session.get(ForumPost.class, 10); Forum forum = forumPost.getForum(); System.out.println(forum.getFid()); System.out.println(forum.getName()); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } } }
本文出自 “素颜” 博客,谢绝转载!
Hibernate5-多对一双向关联-fetch="select",lazy="proxy"
标签:多对一 hibernate5 双向关联
原文地址:http://suyanzhu.blog.51cto.com/8050189/1913286