我的架构中有两个表,它们之间具有一对多的关系。
unit and unit_bridge
单位有2列id和name
unit_bridge有两列,unit_master_id和unit_slave_id。它们两者共同构成一个composite key。这里unit_slave_id是单元中id列的外键。
当我收到POST请求时,正在数据库中创建一个名为unit_child_units的新表。并且unit_bridge的unit_slave_id列值被更新。尽管unit_master_id的值在unit_bridge表中是正确的,但它对应的unit_slave_id映射是错误的。
我不需要新表,而是要更新unit_bridge表,因为我收到了用户对unit表的POST请求。
这是我的json请求,
{
"name" : "john doe",
"childUnits" : [
{
"unitBridgeId" :{
"unit_master_id" : 4
}
}
]
}
这是我的模特。
Unit.java
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="unit")
@JsonInclude(Include.NON_NULL)
public class Unit implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@Transient
private int parent_id;
@OneToMany(cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
private Set<UnitBridge> childUnits = new HashSet<UnitBridge>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getParent_id() {
return parent_id;
}
public void setParent_id(int parent_id) {
this.parent_id = parent_id;
}
public Set<UnitBridge> getChildUnits() {
return childUnits;
}
public void setChildUnits(Set<UnitBridge> childUnits) {
this.childUnits = childUnits;
}
@Override
public String toString() {
return "Unit [id=" + id + ", name=" + name + ", parent_id=" + parent_id + ", childUnits=" + childUnits + "]";
}
}
UnitBridge.java
import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Entity
@Table(name="unit_bridge")
@JsonInclude(Include.NON_NULL)
public class UnitBridge implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@EmbeddedId
private UnitBridgeId unitBridgeId;
@Override
public String toString() {
return "UnitBridge [unitBridgeId=" + unitBridgeId + "]";
}
public UnitBridgeId getUnitBridgeId() {
return unitBridgeId;
}
public void setUnitBridgeId(UnitBridgeId unitBridgeId) {
this.unitBridgeId = unitBridgeId;
}
public int getUnit_master_id() {
return unitBridgeId.getUnit_master_id();
}
public int getUnit_slave_id() {
return unitBridgeId.getUnit_slave_id();
}
public void setUnit_master_id(int unit_master_id) {
unitBridgeId.setUnit_master_id(unit_master_id);
}
public void setUnit_slave_id(int unit_slave_id) {
unitBridgeId.setUnit_slave_id(unit_slave_id);
}
}
UnitBridgeId.java
import java.io.Serializable;
import javax.persistence.Embeddable;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@Embeddable
public class UnitBridgeId implements Serializable {
private static final long serialVersionUID = 1L;
private int unit_master_id;
private int unit_slave_id;
public UnitBridgeId() {
}
public UnitBridgeId(int unit_master_id, int unit_slave_id) {
this.unit_master_id = unit_master_id;
this.unit_slave_id=unit_slave_id;
}
public int getUnit_master_id() {
return unit_master_id;
}
public void setUnit_master_id(int unit_master_id) {
this.unit_master_id = unit_master_id;
}
public int getUnit_slave_id() {
return unit_slave_id;
}
public void setUnit_slave_id(int unit_slave_id) {
this.unit_slave_id = unit_slave_id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + unit_master_id;
result = prime * result + unit_slave_id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UnitBridgeId other = (UnitBridgeId) obj;
if (unit_master_id != other.unit_master_id)
return false;
if (unit_slave_id != other.unit_slave_id)
return false;
return true;
}
@Override
public String toString() {
return "UnitBridgeId [unit_master_id=" + unit_master_id + ", unit_slave_id=" + unit_slave_id + "]";
}
}
ManyToOne
关系,而不是Unit中的OneToMany
,这样就不需要映射表。
@Table(name="unit")
public class Unit implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@Transient
private int parent_id;
//getters and setters
}
@Entity
@Table(name="unit_bridge")
public class UnitBridge implements Serializable{
private static final long serialVersionUID = 1L;
@EmbeddedId
private UnitBridgeId unitBridgeId;
@ManyToOne
private Unit unit;
//getters and setters
}