欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JPA中配置ManyToOne和OneToMany的双向

程序员文章站 2022-04-22 21:56:20
...

关键要记住下面一句话:

多的一方为关系维护端,关系维护端负责外键记录的更新,关系被维护端是没有权利更新外键记录。


注意:(亲身经历哦~~)
我晕,找了好久的错误,发现是自己图方便没有用@Table标注,结果生成表的表名为对应的类名,刚好
是MySQL的关键字group,以后还是最好加上@Table标注,并且表名最好以“t_”开头。

 

关系维护端(多的一方)

package contactbook.domain;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="t_contacts")
public class Contact implements Serializable{

	private static final long serialVersionUID = 5630087178466798365L;
	private Integer id;
	private String name;
	private String phone;
	private String email;
	private String address;
	private Group group;
	
	public Contact() {
		super();
	}
	public Contact(String name, String phone, String email, String address) {
		super();
		this.name = name;
		this.phone = phone;
		this.email = email;
		this.address = address;
	}
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=false)
	@JoinColumn(name="groupid")
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
	@Override
	public String toString() {
		return "name:"+name+",phone:"+phone+",email:"+email+",address:"+address;
	}
}
 

关系被维护端(一的一方)

package contactbook.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="t_groups")
public class Group implements java.io.Serializable{

	private static final long serialVersionUID = -6948198014545065959L;
	
	private Integer id;
	private String name;
	private Set<Contact> contacts = new HashSet<Contact>();
	
	public Group() {
		super();
	}
	public Group(String name) {
		super();
		this.name = name;
	}
	@Id
	@GeneratedValue
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@OneToMany(mappedBy="group",cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH},
	fetch=FetchType.EAGER)
	public Set<Contact> getContacts() {
		return contacts;
	}
	public void setContacts(Set<Contact> contacts) {
		this.contacts = contacts;
	}
	
	//添加方便
	public void addContacts(Contact contact){
		contact.setGroup(this);//通过多端来维护他们的关系,外键在多端
		this.contacts.add(contact);
	}
	@Override
	public String toString() {
		return "id:"+id+",name:"+name;
	}
	
}

  测试:

package contactbook.test;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import contactbook.domain.Contact;
import contactbook.domain.Group;
import contactbook.service.GroupService;

public class TestGroup {
	private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	private GroupService groupService = (GroupService) ac.getBean("groupService");
	
	@Test
	public void testSave(){
		Group g = new Group("同学");
		Contact c1 = new Contact("小红","13729767419","[email protected]","江西");
		Contact c2 = new Contact("刘俊","13753449488","[email protected]","昆明");
		g.addContacts(c1);
		g.addContacts(c2);
		groupService.save(g);
	}
	
	@Test
	public void testFindAll(){
		List<Group> gList = groupService.findAll();
		System.out.println(gList);
		for(Group g:gList){
			System.out.println("id:"+g.getId()+","+g.getContacts());
		}
	}
}
  省略了applicationContext.xml代码,和src/META-INF/persistence.xml和dao和service代码。