欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate 单向、双向 一对一 关联

程序员文章站 2022-04-22 16:11:09
...
------------------------------------------------------------------------------------------
快速搭建 HIBERNATE,以便测试
------------------------------------------------------------------------------------------
person - address

public class Person implements java.io.Serializable {

// Fields

private Long id;
private String name;
private Long age;
private Long addressId;

private Address address ;

------省略get/set方法

public class Address implements java.io.Serializable {

// Fields

private Long id;
private String detail;

------省略get/set方法

hibernate 配置 person
建表语句
CREATE TABLE "SYSTEM"."PERSON"
(
"ID" NUMBER(16,0) NOT NULL ENABLE,
"NAME" VARCHAR2(20),
"AGE" NUMBER(4,0),
"ADDRESS_ID" NUMBER(16,0) NOT NULL ENABLE,
PRIMARY KEY ("ID")
--------------------------
CREATE TABLE "SYSTEM"."ADDRESS"
( "ID" NUMBER(16,0) NOT NULL ENABLE,
"DETAIL" VARCHAR2(200),
PRIMARY KEY ("ID")
-----------------------

<hibernate-mapping>
<class name="com.po.Person" table="PERSON">
<id name="id" type="java.lang.Long">
<column name="ID" precision="16" scale="0" />
<generator class="sequence" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="20" />
</property>
<property name="age" type="java.lang.Long">
<column name="AGE" precision="4" scale="0" />
</property>
<property name="addressId" type="java.lang.Long">
<column name="ADDRESS_ID" precision="16" scale="0" not-null="true" />
</property>

<many-to-one name="address" column="id" unique="true"></many-to-one>
<!-- <one-to-one name="address"></one-to-one> 这么写也可以-->
</class>
</hibernate-mapping>

--------------------------------
hibernate 配置 address

<hibernate-mapping>
<class name="com.po.Address" table="ADDRESS">
<id name="id" type="java.lang.Long">
<column name="ID" precision="16" scale="0" />
<generator class="sequence" />
</id>
<property name="detail" type="java.lang.String">
<column name="DETAIL" length="200" />
</property>
</class>
</hibernate-mapping>


测试
----------------------------------
/**
* Test.java
* com.hib
*
* Function: TODO
*
* ver date author
* ──────────────────────────────────
* Mar 31, 2010 程仁银
*
* Copyright (c) 2010, All Rights Reserved.
*/

package com.hib;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.po.Address;
import com.po.Person;
import com.po.TbUsers;

/**
* ClassName:Test
* Project:
* Company: LINKAGE
*
* @author 程仁银
* @version
* @since Ver 1.1
* @Date Mar 31, 2010 1:56:43 PM
*
* @see
*/
public class Test {

/**@author . 程仁银
* @email . [email protected]
* @createTime .Mar 31, 2010 1:56:44 PM
* @parameters .
* @description .
*/
public static void main(String[] args)
{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
// 业务逻辑测试
Query query=session.createQuery("from Person u");
List<Person> list = query.list();
for(Person person:list)
{
System.out.println("----------"+person.getName()+" "+person.getAddress().getId()+" "+person.getAddress().getDetail());
}
System.out.println("==");

/*Query query=session.createQuery("from Address u");
List<Address> list = query.list();
for(Address p:list)
{
System.out.println(p.getId()+" "+p.getDetail());
}
System.out.println("==");*/

tx.commit();
}

}



双向关联 配置 简概如下:
public class Person implements java.io.Serializable {

// Fields

private Long id;
private String name;
private Long age;
private Long addressId;

private Address address ;
<many-to-one name="address" column="id" unique="true"></many-to-one>

----------------------------------------------------
private Long id;
private String detail;
private Person person;
<many-to-one name="person" column="id" unique="true" fetch="select"></many-to-one>
测试:
Query query=session.createQuery("from Address u");
List<Address> list = query.list();
for(Address p:list)
{
System.out.println(p.getId()+" "+p.getDetail()+" - "+p.getPerson().getName());
}
相关标签: Hibernate Java