JPA 双向多对多关联(@ManyToMany)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > JPA 双向多对多关联(@ManyToMany)

JPA 双向多对多关联(@ManyToMany)

 2010/11/17 22:48:44  nellybady  http://nellybady.javaeye.com  我要评论(0)
  • 摘要:下面仍旧使用客户实体(CustomerEO)与地址实体(AddressEO)为例来说明如何映射多对多关系的映射。一个客户可以有多个地址,一个地址又可以所属多个的客户。客户和地址是对对多的关系,并且地址与客户是双向关联的。实例如下:packagecom.demo.bean.manytomany;importjava.util.ArrayList;importjava.util.Collection;importjavax.persistence.CascadeType;importjavax
  • 标签:JPA 双向多对多关联
    下面仍旧使用客户实体(CustomerEO)与地址实体(AddressEO)为例来说明如何映射多对多关系的映射。
    一个客户可以有多个地址,一个地址又可以所属多个的客户。客户和地址是对对多的关系,并且地址与客户是双向关联的。实例如下:
package com.demo.bean.manytomany;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Version;

/**
 * 
 * manytomany双向,外键关联,
 * @author ALEX
 *
 */
@Entity
@Table(name = "t_customer_mm")
public class Customer_mm {

	@Version
	private Long version;

	@Id
	@GeneratedValue
	private Long id;//客户ID
	
	@Column
	public String name;//客户名称
	
	@ManyToMany(cascade={CascadeType.ALL})
	@JoinTable(name="ref_customer_address",
			
			joinColumns={
				@JoinColumn(name="customer_id",referencedColumnName="id")
				},
				inverseJoinColumns={
			     @JoinColumn(name="address_id",referencedColumnName="id")
				}
	
	)
	public Collection<Address_mm> address = new ArrayList<Address_mm>();
			
	public Customer_mm(){		
	}
	
	//-----------------可省get,set--------------
	
}

package com.demo.bean.manytomany;

import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Version;

/**
 * entity Address_mm 
 * @author ALEX
 *
 */
@Entity
@Table(name="t_address_mm")
public class Address_mm {
	
	@Version
	private Long version;
	
	@Id
	@GeneratedValue
	private Long id;//id
		
	public String province;//省份
	
	public String city;//城市
	
	public String postcode;//邮编
	
	public String detail;//详细地址
	
          /**
          *说明,若为单向关联,则只需要将此注解行删除即可.
          */
	@ManyToMany(mappedBy="address")
	public Collection<Customer_mm> customer = new ArrayList<Customer_mm>();

	//----------get,set可省------------
	
}

package com.demo.bean.manytomany;

import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class TestJPA {

	/**
	 * 测试jpa注解,
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		EntityManagerFactory emf = Persistence.createEntityManagerFactory("tpdb");		
		EntityManager em = emf.createEntityManager();
		EntityTransaction et = em.getTransaction();
		
		et.begin();
		
	    saveOneToOne(em);
	    et.commit();
	    
	    //查询
	    Customer_mm ctm = em.find( Customer_mm.class, new Long(1));
	    Collection cot  = ctm.getAddress();
	    for( Object address_m:cot){
	    	Address_mm am = (Address_mm) address_m;
	    	System.out.println("cm---am --"+am.getDetail());
	    }
	    
	}
		
	private static void saveOneToOne(EntityManager em){
		
		Customer_mm ct = new Customer_mm();
		ct.setName("周董");
		Customer_mm ct1 = new Customer_mm();
		ct1.setName("方文山");
		
		Address_mm ad = new Address_mm();
		ad.setProvince("湖北省");
		ad.setCity("武汉市");
		ad.setPostcode("201561");
		ad.setDetail("中心区");
		
		Address_mm ad1 = new Address_mm();
		ad1.setProvince("西安省");
		ad1.setCity("渭南市");
		ad1.setPostcode("250012");
		ad1.setDetail("大桥区");

		//关联
		ct.getAddress().add( ad );
		ct1.getAddress().add( ad );
		ct1.getAddress().add( ad1 );
		
		ad.getCustomer().add( ct );
		ad1.getCustomer().add( ct1 );
		
	         em.persist( ct );
	         em.persist( ct1 );
		
	}	
}
发表评论
用户名: 匿名