hibernate入门使用系列 8-- annotation关系映射篇(下)

终于要说ManyToMany了

场景:Product和Customer。

 

先看TestProduct.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.ManyToMany;

@Entity
public class TestProduct
{
    private String             id;
    private String             name;
    private float              price;
    private List<TestCustomer> customers = new ArrayList<TestCustomer>();

    /**
     * @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 price
     */
    public float getPrice()
    {
        return price;
    }

    /**
     * @param price the price to set
     */
    public void setPrice(float price)
    {
        this.price = price;
    }

    /**
     * @return the customers
     */
    @ManyToMany
    public List<TestCustomer> getCustomers()
    {
        return customers;
    }

    /**
     * @param customers the customers to set
     */
    public void setCustomers(List<TestCustomer> customers)
    {
        this.customers = customers;
    }

}

 注意这里的ManyToMany什么都没有写。

 

再看TestCustomer.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.ManyToMany;

@Entity
public class TestCustomer
{
    private String            id;
    private String            tel;
    private List<TestProduct> products = new ArrayList<TestProduct>();

    /**
     * @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 tel
     */
    public String getTel()
    {
        return tel;
    }

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

    /**
     * @return the products
     */
    @ManyToMany(mappedBy = "customers")
    public List<TestProduct> getProducts()
    {
        return products;
    }

    /**
     * @param products the products to set
     */
    public void setProducts(List<TestProduct> products)
    {
        this.products = products;
    }
}

 

这里的ManyToMany我写了mappedBy这个attribute。

 

然后看hib产生的sql:

drop table test_customer cascade constraints;
drop table test_product cascade constraints;
drop table test_product_customers cascade constraints;

create table test_customer (
        id varchar2(255 char) not null,
        tel varchar2(255 char),
        primary key (id)
);

create table test_product (
    id varchar2(255 char) not null,
    price float not null,
    name varchar2(255 char),
    primary key (id)
);

create table test_product_customers (
    products_id varchar2(255 char) not null,
    customers_id varchar2(255 char) not null
);
 

ok! 非常好。hib终于在ManyToMany上没有犯白痴了。

 

上面强调了mappedBy这个属性。其实,在annotation 系列中。都有提到mappedBy这个东西。只是,我没有说到底是什么意思。其实很简单:这个东西就相当于xml配置中的inverse。写了mappedBy就代表这个方法的返回值是被维护方。可以看看

hibernate入门使用系列 6-- annotation关系映射篇(上)

的最后2句话。

 

这里,hib的配置方面我想该告一段落了。应该有点其他的东西了。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值