---
title: "hibernate入门使用系列 8-- annotation关系映射篇（下）"
author: "RyanPoy"
date: "2008-11-12T17:27:46Z"
tags: []
slug: "hibernate-8-annotation"
draft: false
lastmod: "2008-11-12T17:29:23Z"
id: 266159
url: "https://ryanpoy.iteye.com/blog/266159"
---

终于要说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关系映射篇（上）](/blog/266091)

的最后2句话。

 

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