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。作为关联。太笨了。

 

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

那我们该如何呢?

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值