Hibernate映射问题之OneToOne【自己整理】

首先贴上一个MkYong的例子

stock.java

 1 package com.mkyong.stock;
 2
 3 import javax.persistence.CascadeType;
 4 import javax.persistence.Column;
 5 import javax.persistence.Entity;
 6 import javax.persistence.FetchType;
 7 import javax.persistence.GeneratedValue;
 8 import static javax.persistence.GenerationType.IDENTITY;
 9 import javax.persistence.Id;
10 import javax.persistence.OneToOne;
11 import javax.persistence.Table;
12 import javax.persistence.UniqueConstraint;
13
14 @Entity
15 @Table(name = "stock", catalog = "mkyongdb", uniqueConstraints = {
16         @UniqueConstraint(columnNames = "STOCK_NAME"),
17         @UniqueConstraint(columnNames = "STOCK_CODE") })
18 public class Stock implements java.io.Serializable {
19
20     private Integer stockId;
21     private String stockCode;
22     private String stockName;
23     private StockDetail stockDetail;
24
25     public Stock() {
26     }
27
28     public Stock(String stockCode, String stockName) {
29         this.stockCode = stockCode;
30         this.stockName = stockName;
31     }
32
33     public Stock(String stockCode, String stockName, StockDetail stockDetail) {
34         this.stockCode = stockCode;
35         this.stockName = stockName;
36         this.stockDetail = stockDetail;
37     }
38
39     @Id
40     @GeneratedValue(strategy = IDENTITY)
41     @Column(name = "STOCK_ID", unique = true, nullable = false)
42     public Integer getStockId() {
43         return this.stockId;
44     }
45
46     public void setStockId(Integer stockId) {
47         this.stockId = stockId;
48     }
49
50     @Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)
51     public String getStockCode() {
52         return this.stockCode;
53     }
54
55     public void setStockCode(String stockCode) {
56         this.stockCode = stockCode;
57     }
58
59     @Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)
60     public String getStockName() {
61         return this.stockName;
62     }
63
64     public void setStockName(String stockName) {
65         this.stockName = stockName;
66     }
67
68     @OneToOne(fetch = FetchType.LAZY, mappedBy = "stock", cascade = CascadeType.ALL)
69     public StockDetail getStockDetail() {
70         return this.stockDetail;
71     }
72
73     public void setStockDetail(StockDetail stockDetail) {
74         this.stockDetail = stockDetail;
75     }
76
77 }

StockDetail.java

  1 package com.mkyong.stock;
  2
  3 import java.util.Date;
  4 import javax.persistence.Column;
  5 import javax.persistence.Entity;
  6 import javax.persistence.FetchType;
  7 import javax.persistence.GeneratedValue;
  8 import javax.persistence.Id;
  9 import javax.persistence.OneToOne;
 10 import javax.persistence.PrimaryKeyJoinColumn;
 11 import javax.persistence.Table;
 12 import javax.persistence.Temporal;
 13 import javax.persistence.TemporalType;
 14 import org.hibernate.annotations.GenericGenerator;
 15 import org.hibernate.annotations.Parameter;
 16
 17 @Entity
 18 @Table(name = "stock_detail", catalog = "mkyongdb")
 19 public class StockDetail implements java.io.Serializable {
 20
 21     private Integer stockId;
 22     private Stock stock;
 23     private String compName;
 24     private String compDesc;
 25     private String remark;
 26     private Date listedDate;
 27
 28     public StockDetail() {
 29     }
 30
 31     public StockDetail(Stock stock, String compName, String compDesc,
 32             String remark, Date listedDate) {
 33         this.stock = stock;
 34         this.compName = compName;
 35         this.compDesc = compDesc;
 36         this.remark = remark;
 37         this.listedDate = listedDate;
 38     }
 39
 40     @GenericGenerator(name = "generator", strategy = "foreign",
 41     parameters = @Parameter(name = "property", value = "stock"))
 42     @Id
 43     @GeneratedValue(generator = "generator")
 44     @Column(name = "STOCK_ID", unique = true, nullable = false)
 45     public Integer getStockId() {
 46         return this.stockId;
 47     }
 48
 49     public void setStockId(Integer stockId) {
 50         this.stockId = stockId;
 51     }
 52
 53     @OneToOne(fetch = FetchType.LAZY)
 54     @PrimaryKeyJoinColumn
 55     public Stock getStock() {
 56         return this.stock;
 57     }
 58
 59     public void setStock(Stock stock) {
 60         this.stock = stock;
 61     }
 62
 63     @Column(name = "COMP_NAME", nullable = false, length = 100)
 64     public String getCompName() {
 65         return this.compName;
 66     }
 67
 68     public void setCompName(String compName) {
 69         this.compName = compName;
 70     }
 71
 72     @Column(name = "COMP_DESC", nullable = false)
 73     public String getCompDesc() {
 74         return this.compDesc;
 75     }
 76
 77     public void setCompDesc(String compDesc) {
 78         this.compDesc = compDesc;
 79     }
 80
 81     @Column(name = "REMARK", nullable = false)
 82     public String getRemark() {
 83         return this.remark;
 84     }
 85
 86     public void setRemark(String remark) {
 87         this.remark = remark;
 88     }
 89
 90     @Temporal(TemporalType.DATE)
 91     @Column(name = "LISTED_DATE", nullable = false, length = 10)
 92     public Date getListedDate() {
 93         return this.listedDate;
 94     }
 95
 96     public void setListedDate(Date listedDate) {
 97         this.listedDate = listedDate;
 98     }
 99
100 }


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mkyongdb</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <mapping class="com.mkyong.stock.Stock" />
    <mapping class="com.mkyong.stock.StockDetail" />
</session-factory>
</hibernate-configuration>

  然后是用于测试的App.java

package com.mkyong;

import java.util.Date;

import org.hibernate.Session;

import com.mkyong.stock.Stock;
import com.mkyong.stock.StockDetail;
import com.mkyong.util.HibernateUtil;

public class App {
	public static void main(String[] args) {
		System.out.println("Hibernate one to one (Annotation)");
		Session session = HibernateUtil.getSessionFactory().openSession();

		session.beginTransaction();

		Stock stock = new Stock();

		stock.setStockCode("7052");
		stock.setStockName("PADINI");

		StockDetail stockDetail = new StockDetail();
		stockDetail.setCompName("PADINI Holding Malaysia");
		stockDetail.setCompDesc("one stop shopping");
		stockDetail.setRemark("vinci vinci");
		stockDetail.setListedDate(new Date());

		stock.setStockDetail(stockDetail);
		stockDetail.setStock(stock);

		session.save(stock);
		session.getTransaction().commit();

		System.out.println("Done");
	}
}

  

时间: 2024-10-04 08:35:00

Hibernate映射问题之OneToOne【自己整理】的相关文章

Hibernate 映射关系

映射组成关系 •建立域模型和关系数据模型有着不同的出发点: –域模型: 由程序代码组成, 通过细化持久化类的的粒度可提高代码的可重用性, 简化编程 –在没有数据冗余的情况下, 应该尽可能减少表的数目, 简化表之间的参照关系, 以便提高数据的访问速度 •Hibernate 把持久化类的属性分为两种: –值(value)类型: 没有 OID, 不能被单独持久化, 生命周期依赖于所属的持久化类的对象的生命周期 –实体(entity)类型: 有 OID, 可以被单独持久化, 有独立的生命周期 •显然无法

坑爹的Hibernate 映射文件错误提示org.xml.sax.SAXParseException

今天整整一个上午都在和hibernate做斗争,早上一来,继续昨天的项目开发,发现spring项目不能启动,从错误中看是hibernate错误,多半是hibernate配置有错误,关键是错误提示中显示“com.mdnet.travel.core.dao.ValidCodeDAOImpl”不能注入,经过仔细查看未见异常,而且这个文件好久没有改过了.没办法把hibernate的配置改成如下配置: <bean id="sessionFactory" class="org.sp

hibernate映射的 关联关系:有 一对多关联关系,一对一关联关系,多对多关联关系,继承关系

hibernate环境配置:导包.... 单向n-1:单向 n-1 关联只需从 n 的一端可以访问 1 的一端 <many-to-one> 元素来映射组成关系: name: 设定待映射的持久化类的属性的名字 column: 设定和持久化类的属性对应的表的外键 class:设定待映射的持久化类的属性的类型 建立1所对应的封装类,这儿只写属性,setter和getter就不写了: private Integer customerId; private String customerName; 建立

【SSH进阶之路】Hibernate映射——一对一双向关联映射(六)

上篇博文[SSH进阶之路]Hibernate映射--一对一单向关联映射(五),我们介绍了一对一的单向关联映射,单向是指只能从人(Person)这端加载身份证端(IdCard),但是反过来,不能从身份证端加载人得信息.如图所示: 关键原因在于对象模型具有方向性: 单向:一端只能加载另一端,不能反过来. 双向:两端都可以加载另一端. 问题来了:如何我们想从身份证端(IdCard)加载人(Person),怎么办呢? 下面我们开始介绍一对一的双向关联映射. 映射原理 双向关联映射与单向关联映射的原理是一

Hibernate映射解析——七种映射关系

        ORM,全称是(Object Relational Mapping),即对象关系映射.ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现,这样开发人员就可以把对数据库的操作转化为对这些对象的操作.Hibernate正是实现了这种思想,达到了方便开发人员以面向对象的思想来实现对数据库的操作.    Hibernate在实现ORM功能的时候主要用到的文件有:映射类(*.java).映射文件(*.hbm.xml)和数据库配置文件(*.properties/*.cfg

Hibernate映射关系

1.主键相同的一对一关系(商店与商家账户的管理) 两个实体类使用相同的主键,反过来,具有相同主键的尸体被视为一对一的关系.这样就省掉外键关联. 商店类: @Entity @Table(name="tb_store") public class Store { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Integer id; /** * 区信息之外的详细商店地址 */ @Column(nullable=false)

【SSH进阶之路】Hibernate映射——一对一单向关联映射(五)

[SSH进阶之路]Hibernate基本原理(一) ,小编介绍了Hibernate的基本原理以及它的核心,采用对象化的思维操作关系型数据库. [SSH进阶之路]Hibernate搭建开发环境+简单实例(二),小编搭建了基本Hibernate的开发环境,并做了一个简单实例,对它的基本原理有了一个理性的认识. [SSH进阶之路]Hibernate基本映射(三),我们介绍了Hibernate的基本映射(即对一个实体进行映射)的相关概念,并给大家实现相关实例,比较简单. [SSH进阶之路]Hiberna

hibernate映射-基于主键映射的1-1关联关系

(学习记录,错误不足之处,请您耐心指正^_^) hibernate映射-基于主键映射的1-1关联关系 基于主键的映射策略:指一端的主键生成器使用foreign策略,表明根据对方的主键来生成自己的主键,自己并不独立生成主键. 一.代码示例: {类文件↓} Manager.class 1 package com.zit.hibernate.one2one.primary; 2 3 public class Manager { 4 5 private Integer mgrId; 6 private

009一对一 主键关联映射_单向(one-to-one)

009一对一  主键关联映射_单向(one-to-one) ²  两个对象之间是一对一的关系,如Person-IdCard(人—身份证号) ²  有两种策略可以实现一对一的关联映射 主键关联:即让两个对象具有相同的主键值,以表明它们之间的一一对应的关系:数据库表不会有额外的字段来维护它们之间的关系,仅通过表的主键来关联. 唯一外键关联:外键关联,本来是用于多对一的配置,但是如果加上唯一的限制之后,也可以用来表示一对一关联关系. 实例场景:人—-> 身份证号(PersonàIdCard),从IdC