`
RyanPoy
  • 浏览: 50473 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

hibernate入门使用系列 7-- annotation关系映射篇(中)

阅读更多

这次说说OneToMany和ManyToOne

 

我们的场景是 1个父亲n多个孩子

 

先来看看这中做法:

TestFather.java

package net.paoding.forum.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class TestFather
{
    String          id;
    String          name;
    List<TestChild> children = new ArrayList<TestChild>();

    /**
     * @return the id
     */
    @Id
    public String getId()
    {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id)
    {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name)
    {
        this.name = name;
    }

    /**
     * @return the children
     */
    @OneToMany
    public List<TestChild> getChildren()
    {
        return children;
    }

    /**
     * @param children the children to set
     */
    public void setChildren(List<TestChild> children)
    {
        this.children = children;
    }
}
 

 

TestChild.java

package net.paoding.forum.domain;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class TestChild
{
    String     id;
    String     name;

    /**
     * @return the id
     */
    @Id
    public String getId()
    {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id)
    {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name)
    {
        this.name = name;
    }
}

 

这样子,好像就可以了。那么是的么?我们来看看hib产生的sql

drop table test_child cascade constraints;
drop table test_father cascade constraints;
drop table test_father_children cascade constraints;

create table test_child (
    id varchar2(255 char) not null,
    name varchar2(255 char),
    primary key (id)
);

create table test_father (
    id varchar2(255 char) not null,
    name varchar2(255 char),
    primary key (id)
);

create table test_father_children (
    null_id varchar2(255 char) not null,
    children_id varchar2(255 char) not null,
    unique (children_id)
);


alter table test_father_children 
    add constraint FK2E6E87D5901B7DBB 
    foreign key (null_id) 
    references test_father;

alter table test_father_children 
    add constraint FK2E6E87D58CD0E56B 
    foreign key (children_id) 
    references test_child;

alter table test_user 
    add constraint FKB9A96B58A237D846 
    foreign key (card_id) 
    references test_card;

 oh! My God !它居然又帮我们多创建了一个table。作为关联。太笨了。

 

很明显,这不是我们所需要的。

那我们该如何呢?

 

分享到:
评论
3 楼 RyanPoy 2008-11-12  
由此我个人给出的结论是: 如果想通过hibernate的 po->ddl 这个功能,在ManyToOne的时候,一定要加上OneToMany配合使用。这样子,才能达到预想的效果。
2 楼 RyanPoy 2008-11-12  
接上
总不能放弃吧。再来。这次我们修改一下TestFather.java

package net.paoding.forum.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class TestFather
{
    String          id;
    String          name;
    List<TestChild> children = new ArrayList<TestChild>();

    /**
     * @return the id
     */
    @Id
    public String getId()
    {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id)
    {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name)
    {
        this.name = name;
    }

    /**
     * @return the children
     */
    @OneToMany(mappedBy="father")
    public List<TestChild> getChildren()
    {
        return children;
    }

    /**
     * @param children the children to set
     */
    public void setChildren(List<TestChild> children)
    {
        this.children = children;
    }
}

注意@OneToMany, 我们加上了 mappedBy="father", 这次看看sql吧:
   drop table test_child cascade constraints;
drop table test_father cascade constraints;

create table test_child (
        id varchar2(255 char) not null,
        name varchar2(255 char),
        father_id varchar2(255 char),
        primary key (id)
    );

create table test_father (
    id varchar2(255 char) not null,
    name varchar2(255 char),
    primary key (id)
);

alter table test_child 
        add constraint FK7A81672F268831C6 
        foreign key (father_id) 
        references test_father;

alter table test_user 
    add constraint FKB9A96B58A237D846 
    foreign key (card_id) 
    references test_card;

我的天啊!终于对了。累死了。这么说,难道就没有实现单向的么?只能通过xml配置?hib啊,你还是比较难用。
1 楼 RyanPoy 2008-11-12  
接上。
于是,我们修改代码:
TestChild.java
package net.paoding.forum.domain;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class TestChild
{
    String     id;
    String     name;
    TestFather father;

    /**
     * @return the id
     */
    @Id
    public String getId()
    {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id)
    {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name)
    {
        this.name = name;
    }

    /**
     * @return the father
     */
    @ManyToOne
    public TestFather getFather()
    {
        return father;
    }

    /**
     * @param father the father to set
     */
    public void setFather(TestFather father)
    {
        this.father = father;
    }
}


那么,这次产生的sql呢?
drop table test_child cascade constraints;
drop table test_father cascade constraints;
drop table test_father_children cascade constraints;

create table test_child (
    id varchar2(255 char) not null,
    name varchar2(255 char),
    father_id varchar2(255 char),
    primary key (id)
);

create table test_father (
    id varchar2(255 char) not null,
    name varchar2(255 char),
    primary key (id)
);

create table test_father_children (
    null_id varchar2(255 char) not null,
    children_id varchar2(255 char) not null,
    unique (children_id)
);	

alter table test_child 
    add constraint FK7A81672F268831C6 
    foreign key (father_id) 
    references test_father;

alter table test_father_children 
    add constraint FK2E6E87D5901B7DBB 
    foreign key (null_id) 
    references test_father;

alter table test_father_children 
    add constraint FK2E6E87D58CD0E56B 
    foreign key (children_id) 
    references test_child;

alter table test_user 
    add constraint FKB9A96B58A237D846 
    foreign key (card_id) 
    references test_card;



唉!失望了!?hib真TMD愚蠢。

相关推荐

    hibernate3.2中文文档(chm格式)

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    HibernateAPI中文版.chm

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    Hibernate+中文文档

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    Hibernate中文详细学习文档

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    Hibernate 中文 html 帮助文档

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    Hibernate O/R Mapping 入门工程包

    Hibernate 快速入门的工程包 包括用xml和annotation映射数据表

    hibernate 体系结构与配置 参考文档(html)

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    Hibernate教程

    2. Hibernate入门 2.1. 前言 2.2. 第一部分 - 第一个Hibernate程序 2.2.1. 第一个class 2.2.2. 映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. 第二部分 ...

    hibernate3.04中文文档.chm

    2. Hibernate入门 2.1. 前言 2.2. 第一部分 - 第一个Hibernate程序 2.2.1. 第一个class 2.2.2. 映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. ...

    Hibernate3的帮助文档

    2. Hibernate入门 2.1. 前言 2.2. 第一部分 - 第一个Hibernate程序 2.2.1. 第一个class 2.2.2. 映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. 第二部分 ...

    Hibernate参考文档

    1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第...

    hibernate 框架详解

    2. Hibernate入门 2.1. 前言 2.2. 第一部分 - 第一个Hibernate程序 2.2.1. 第一个class 2.2.2. 映射文件 2.2.3. Hibernate配置 2.2.4. 用Ant编译 2.2.5. 安装和帮助 2.2.6. 加载并存储对象 2.3. 第二...

    Hibernate4入门基础学习视频课程

    Hibernate原理的模拟,重点讲思路,代码不全部实现,Hibernate的基本配置 Hibernate核心接口介绍,对象的三种状态,Hibernate关系映射:一对一、 一对多 、 多对多。 Hibernate查询(HQL语句的使用)。

    经典JAVA.EE企业应用实战.基于WEBLOGIC_JBOSS的JSF_EJB3_JPA整合开发.pdf

     本书是《轻量级java ee企业应用实战》的姊妹篇,《轻量级java ee企业应用实战》主要介绍以spring+hibernate为基础的java ee应用;本书则主要介绍以ejb 3+jpa为基础的java ee应用。ejb 3、jpa规范都属于sun公司所...

    HibernateValidatorJSR303的参考实现使用指南.pdf

    HibernateValidatorJSR303的参考实现使用指南.pdf JSR 303 的参考实现 使用指南 由 Hardy Ferentschik和Gunnar Morling and thanks to Shaozhuang Liu 4.3.1.Final 版权 © 2009 - 2011 Red Hat, Inc. & Gunnar ...

    Spring.3.x企业应用开发实战(完整版).part2

    12.2 在Spring中使用Hibernate 12.2.1 配置SessionFactory 12.2.2 使用HibernateTemplate 12.2.3 处理LOB类型数据 12.2.4 添加Hibernate事件监听器 12.2.5 使用原生Hibernate API 12.2.6 使用注解配置 12.2.7 事务...

    Spring3.x企业应用开发实战(完整版) part1

    12.2 在Spring中使用Hibernate 12.2.1 配置SessionFactory 12.2.2 使用HibernateTemplate 12.2.3 处理LOB类型数据 12.2.4 添加Hibernate事件监听器 12.2.5 使用原生Hibernate API 12.2.6 使用注解配置 12.2.7 事务...

    java开源包7

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

    java开源包1

    开发它是用于在UTF-8 Oracle实例中使用ASCII编码的Oracle 数据库中来正确的传输非ASCII字符。 Java模板语言 Beetl Beetl,是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,...

Global site tag (gtag.js) - Google Analytics