所以我有簡單的關係客戶端與他的類型。由於每個實體都通過id和源系統ID進行標識,因此我有無處不在的組合鍵。如何在保存客戶端實體時自動保存類型?休眠映射字段作爲主鍵和外鍵
Client類:
public class Client implements Serializable {
@EmbeddedId
private ClientKey primaryKey;
@ManyToOne
@JoinColumns({
@JoinColumn(insertable = false, updatable = false, name = "client_type", referencedColumnName = "dict_id"),
@JoinColumn(insertable = false, updatable = false, name = "client_own_id", referencedColumnName = "dict_own_id"),
})
private Type type;
}
主鍵
@Embeddable
public class ClientKey implements Serializable {
@Column(name = "client_id")
private String clientId;
@Column(name = "client_own_id")
private String clientOwnId;
}
型類
@Entity
public class Type implements Serializable {
@EmbeddedId
private DictKey primaryKey; //dict_id and dict_own_id
...
}
當我運行代碼:
clientRepo.create(client); //client contains setted type as typeRepo.getById(1).
交易完成後,我保存了客戶端,但client_type列包含null。從我所瞭解的問題是可插入/可更新= false。所以我設置好的
@JoinColumn(insertable = true, updatable = true, name = "client_type", referencedColumnName = "dict_id"), //true since I want to have updated client_type field
@JoinColumn(insertable = false, updatable = false, name = "client_own_id", referencedColumnName = "dict_own_id"), //false since I have this field in primary key already
然後我得到:
Mixing insertable and non insertable columns in a property is not allowed
,所以我都設置爲true:
@JoinColumn(insertable = true, updatable = true, name = "client_type", referencedColumnName = "dict_id"), //true since I want to have saved client_type field
@JoinColumn(insertable = true, updatable = true, name = "client_own_id", referencedColumnName = "dict_own_id"), //true because I dont know why :D
則:
repeated column in mapping for entity: Client column: client_own_id (should be mapped with insert="false" update="false")
我沒有更多的想法...
編輯:下面 看起來不錯,只是當我有關係的客戶端實體:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(insertable = false, updatable = false, name = "client_id", referencedColumnName = "client_id"),
@JoinColumn(insertable = false, updatable = false, name = "client_own_id", referencedColumnName = "client_own_id"),
})
private Client client;
我'越來越
Unable to find column with logical name: client_own_id in client
[報價]交易我都保存在客戶機之後,但client_status列包含null [end quote]代碼中的client_status列在哪裏? – borowis
sry我拼錯了。狀態=類型。我修正了它 – user2771738