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;
@Before
public void init(){
tmd = new TagModelDAO();
urtmd = new UserResourceTagModelDAO();
tm = new TagModel();
tm.setName("test");
urtm = new UserResourceTagModel();
}
@After
public void destroy(){
tmd.deleteByPrimaryKey(tmId);//这里删除只调用一个就行了,因为在声明一对多的时候声明了是级联删除
}
@Test
public 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无法识别,导致异常抛出。