JPA中配置ManyToOne和OneToMany的双向
关键要记住下面一句话:
多的一方为关系维护端,关系维护端负责外键记录的更新,关系被维护端是没有权利更新外键记录。
注意:(亲身经历哦~~)
我晕,找了好久的错误,发现是自己图方便没有用@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代码。
相关推荐
世樹 2020-11-11
SCNUHB 2020-11-10
bleach00 2020-11-10
FellowYourHeart 2020-10-05
momode 2020-09-11
思君夜未眠 2020-09-04
jessieHJ 2020-08-19
行吟阁 2020-08-09
表格的现在还是较为常用的一种标签,但不是用来布局,常见处理、显示表格式数据。在HTML网页中,要想创建表格,就需要使用表格相关的标签。<table> <tr> <td>单元格内的文字</td> ...
gufudhn 2020-08-09
末点 2020-08-03
nimeijian 2020-07-30
好记忆也需烂 2020-07-28
zlsdmx 2020-07-05
tomson 2020-07-05
tianqi 2020-07-05
onlykg 2020-07-04