所以我有简单的关系客户端与他的类型。由于每个实体都通过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