2016-11-28 129 views
0

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

[报价]交易我都保存在客户机之后,但client_status列包含null [end quote]代码中的client_status列在哪里? – borowis

+0

sry我拼错了。状态=类型。我修正了它 – user2771738

回答

0

看起来你这里是什么就是客户端有一个情况一个复杂的密钥,部分来源于它与Type的关联。

在这种情况下,我觉得映射应如下:

实体类:

public class Client implements Serializable { 

    @EmbeddedId 
    private ClientKey primaryKey; 

    @MapsId("type") //maps to type in the EmbdeddedId 
    @ManyToOne 
    @JoinColumns({ 
     @JoinColumn(name = "client_type", referencedColumnName = "dict_id"), 
     @JoinColumn(name = "client_own_id", referencedColumnName = "dict_own_id") 
    }) 
    private Type type; 
} 

的EmbeddedId:

@Embeddable 
public class ClientKey implements Serializable { 
    @Column(name="client_id") 
    private String clientId; 

    @Embedded 
    private DictKey type; 

    //... 
} 
+0

无法在客户端中找到逻辑名为client_own_id的列;/ 主键中的嵌入式字典键不应覆盖client_type,client_own_id的名称? – user2771738

+0

这是一个问题,事实陈述,错误信息还是什么? –

+0

这是错误。我将客户端和主键中每次出现的client_own_id都更改为client_own_id2,并且仍然无法通过client_own_id。所以任何实体和客户端之间的关系现在都被制动了,因为关系不会看到client_own_id列 – user2771738