【JPA】JPA映射之One to Many
来源:互联网 发布:xls打开软件 编辑:程序博客网 时间:2024/06/05 18:05
JPA中一对多的关系与entity的书写:
一对多关系指的是一个表格中的元素可以对应另一种的多个元素。在我的应用中一对多的关系是:一个标签可以标记多种物品。这是典型的一对多关系
直接代码:
一对多关系中的 “一”的entity注解
package com.free4lab.tag.model;import static javax.persistence.GenerationType.IDENTITY;import java.sql.Timestamp;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.OneToMany;import javax.persistence.Table;/** * 定义一个tag的数据对象 * * @author Administrator * */@Entity@Table(name = "tag_model", catalog = "freetag")public class TagModel {@Id@GeneratedValue(strategy = IDENTITY)@Column(name = "id", unique = true, nullable = false)private Integer id;@Column(name = "tag_name", nullable = false)private String name; //一对多的注解书写格式 //cascade 标识级联方式 //mappedBy是一对多关系中多的一方的声明 @OneToMany(cascade = CascadeType.ALL, mappedBy = "tm")private Set<UserResourceTagModel> urtm = new HashSet<UserResourceTagModel>(); public void add(UserResourceTagModel urtm){ urtm.setTm(this); this.urtm.add(urtm); }public Set<UserResourceTagModel> getUrtm() {return urtm;}public void setUrtm(Set<UserResourceTagModel> urtm) {this.urtm = urtm;}public TagModel() {}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;}}
一对多关系中 “多”的entity注解
package com.free4lab.tag.model;import static javax.persistence.GenerationType.IDENTITY;/** * 记录user、tag、resourece关系 用来记录用户对某个资源打过什么标签 * * @author Administrator * */@Entity@Table(name = "user_resource_tag", catalog = "freetag")public class UserResourceTagModel {@Id@GeneratedValue(strategy = IDENTITY)@Column(name = "id", unique = true, nullable = false)private Integer id;//标识一对多关系的注解,声明对应的“一”是什么 //tag_id为数据库中的字段名称,是标签中唯一标识id对应的数据库字段 @ManyToOne@JoinColumn(name = "tag_id")private TagModel tm;public TagModel getTm() {return tm;}public void setTm(TagModel tm) {this.tm = tm;}public UserResourceTagModel() {}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}}
测试代码:
package com.freetag.model;import org.junit.After;import org.junit.Before;import org.junit.Test;import com.free4lab.tag.model.TagModel;import com.free4lab.tag.model.TagModelDAO;import com.free4lab.tag.model.UserResourceTagModel;import com.free4lab.tag.model.UserResourceTagModelDAO;public class SaveModelTest {private Integer tmId;private TagModel tm;private UserResourceTagModel urtm;TagModelDAO tmd;UserResourceTagModelDAO urtmd;@Beforepublic void init(){tmd = new TagModelDAO();urtmd = new UserResourceTagModelDAO();tm = new TagModel();tm.setName("test");urtm = new UserResourceTagModel();}@Afterpublic void destroy(){tmd.deleteByPrimaryKey(tmId);//这里删除只调用一个就行了,因为在声明一对多的时候声明了是级联删除}@Testpublic void saveTest(){tmd.save(tm);tm.add(urtm);urtmd.save(urtm);}}
如果想单独删除一对多的映射中“多”的表格中某一条记录的时候有两种方式
1:创建本地sql,createNativeSql,通过sql语言删除;
2:通过jpa语言删除,但是必须构造一个对象删除。比如要删除tag_id为2的某条UserResourceTagModel ,则你必须构造一个TagModel传进去,如果单独传tag_id,则jpa无法识别,导致异常抛出。
- 【JPA】JPA映射之One to Many
- JPA中one-to-one关系的单向映射示例
- JPA注解实现one-to-one的主键关联映射
- JPA中one-to-one关系的单向映射示例
- Many-to-One映射
- Many-to-One映射
- Many-to-One映射
- Many-to-One映射
- JPA -the annotation about one to one
- nhibernate: one-to-many映射
- hibernate annotation对jpa主键one-to-one映射关系支持的有问题
- JPA 中 one-toone 一对一 映射
- Hibernate one-to-many / many-to-one关系映射
- Hibernate one-to-many / many-to-one关系映射
- Hibernate one-to-many && many-to-many 的关系映射
- spring-data-jpa 中,如果使用了one-to-many , many-to-one的注释,会在Jackson进行json字符串化的时候出现错误
- Hibernate JPA 之关联映射
- JPA 映射
- hibernate刷新数据时的缓存问题
- HashSet HashMap Hashtable
- LinkList、ArrayList、Vector区别;及 Set
- 健康的使用鼠标
- JAVA反射机制
- 【JPA】JPA映射之One to Many
- 编程之美2.3题目,寻找最大的发帖水王
- Java中的instanceof关键字
- js中的constructor 和prototype深入分析
- eclipse中无法查看源码
- Android开发中的Handler和多线程
- 交换两个变量的值(不借助于第三个变量)
- SIGHUP
- VisionMobile:电信运营商创新工具箱(六)第四章:不对称的商务模型