以下是两个实体类。子类具有单向@ManyToOne
映射。
PlantDetails:
@Entity
@Table(name = "PLANT_DETAILS", uniqueConstraints={ @UniqueConstraint(columnNames = {"PLANT"})})
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler", "ignoreUnknown = true"})
public class PlantDetails implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "PLANT_ID")
private Long plantId;
@Column(name = "PLANT")
private String plant;
@Column(name = "PLANT_DETAILS")
private String plant_Details;
@Column(name = "LOCATION")
private String location;
@JsonCreator
public PlantDetails( @JsonProperty("plantId") Long plantId,
@JsonProperty("plant") String plant,
@JsonProperty("plant_Details") String plant_Details,
@JsonProperty("location") String location )
{
this.plantId = plantId;
this.plant = plant;
this.plant_Details = plant_Details;
this.location = location;
}
LineDetails:
@Entity
@Table(name = "LINE_DETAILS", uniqueConstraints={ @UniqueConstraint(columnNames = { "PLANT", "LINE_NAME" })})
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler", "ignoreUnknown = true"})
public class LineDetails implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "LINE_ID")
private Long lineId;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="PLANT", referencedColumnName = "PLANT")
private PlantDetails plantDetails;
@Column(name = "LINE_NAME")
private String lineName;
@Column(name = "LINE_DESCRIPTION")
private String lineDescription;
@JsonCreator
public LineDetails( @JsonProperty("lineId") Long lineId,
@JsonProperty("lineName") String lineName,
@JsonProperty("lineDescription") String lineDescription) {
this.lineId = lineId;
this.lineName = lineName;
this.lineDescription = lineDescription;
}
我正在尝试进行基本的CRUD操作。以下代码更新子表。我可以使用setter更新列。但我无法更新@JoinColumn
字段“plant”,因为我在实体类的@jsoncreator
构造函数中没有它。如何更新该字段?
在创建/插入记录时,我通过从plantdetails实体ID列获取其值而不是@JoinColumn
来设置@jsoncreator
字段“plant”。
在DAOimpl
@Override
public void updateLineDetails(LineDetails lineDetails) {
Session session = this.sessionFactory.getCurrentSession();
LineDetails existinglineDetails = (LineDetails) session.get(LineDetails.class,
lineDetails.getLineId());
existinglineDetails.setLineName(lineDetails.getLineName());
existinglineDetails.setLineDescription(lineDetails.getLineDescription());
session.update(existinglineDetails);
}
答案 0 :(得分:1)
您必须以某种方式传递此字段所代表的值:
@Column(name = "PLANT")
private String plant;
我通常不会通过实体结构本身传递数据。我会创建额外的LineDetailsInfo
类,它将保存普通字段中的所有信息。
public class LineDetailsInfo{
@JsonCreator
public LineDetails( @JsonProperty("lineId") Long lineId,
@JsonProperty("lineName") String lineName,
@JsonProperty("lineDescription") String lineDescription,
@JsonProperty("plant") String plant) {
this.plant = plant;
this.lineId = lineId;
this.lineName = lineName;
this.lineDescription = lineDescription;
}
}
然后,您需要查询该特定PlantDetails
实体并相应地更新父实体:
@Override
public void updateLineDetails(LineDetailsInfo lineDetailsInfo) {
Session session = this.sessionFactory.getCurrentSession();
PlantDetails pd = (PlantDetails)session.createQuery("from PlantDetails where plant = :plant")
.setString("plant", lineDetailsInfo.getPlant()).uniqueResult();
LineDetails existinglineDetails = (LineDetails) session.get(LineDetails.class,
lineDetailsInfo.getLineId());
existinglineDetails.setLineName(lineDetailsInfo.getLineName());
existinglineDetails.setPlantDetails(pd);
existinglineDetails.setLineDescription(lineDetailsInfo.getLineDescription())
session.update(existinglineDetails);
}