2016-11-28 98 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