【发布时间】:2016-11-28 15:01:01
【问题描述】:
所以我与他的类型有简单的关系客户。由于每个实体都由 id 和源系统 id 标识,因此我到处都有复合键。保存客户端实体时如何自动保存类型?
客户端类:
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。据我了解,问题是可插入/可更新=假。所以我设置了
@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
【问题讨论】:
-
[quote] 交易后我保存了客户端,但 client_status 列包含 null [end quote] 你的代码中的 client_status 列在哪里?
-
对不起,我拼错了。状态 = 类型。我修好了