@ManyToMany- annotation关系映射篇(下)

终于要说ManyToMany了
场景:Product和Customer。
 
先看TestProduct.java
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>();   
  
      
    @Id  
    public String getId()   
    {   
        return id;   
    }   
  
      
    public void setId(String id)   
    {   
        this.id = id;   
    }   
  
      
    public String getName()   
    {   
        return name;   
    }   
  
      
    public void setName(String name)   
    {   
        this.name = name;   
    }   
  
      
    public float getPrice()   
    {   
        return price;   
    }   
  
      
    public void setPrice(float price)   
    {   
        this.price = price;   
    }   
  
      
    @ManyToMany  
    public List<TestCustomer> getCustomers()   
    {   
        return customers;   
    }   
  
      
    public void setCustomers(List<TestCustomer> customers)   
    {   
        this.customers = customers;   
    }   
  
}  
 注意这里的ManyToMany什么都没有写。
 
再看TestCustomer.java
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>();   
  
      
    @Id  
    public String getId()   
    {   
        return id;   
    }   
  
      
    public void setId(String id)   
    {   
        this.id = id;   
    }   
  
      
    public String getTel()   
    {   
        return tel;   
    }   
  
      
    public void setTel(String tel)   
    {   
        this.tel = tel;   
    }   
  
      
    @ManyToMany(mappedBy = "customers")   
    public List<TestProduct> getProducts()   
    {   
        return products;   
    }   
  
      
    public void setProducts(List<TestProduct> products)   
    {   
        this.products = products;   
    }   
}  
 
这里的ManyToMany我写了mappedBy这个attribute。
 
然后看hib产生的sql:
Java代码 
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就代表这个方法的返回值是被维护方
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值