Hibernate映射字段作为主键和外键

时间:2016-11-28 15:01:01

标签: java hibernate

所以我有一个简单的关系客户端和他的类型。由于每个实体都由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。据我所知,问题是insertable / updatable = 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

所以我将两者都设为真:

 @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

1 个答案:

答案 0 :(得分:0)

看起来你在这里遇到的情况是客户有一个复杂的Key,它部分地来自它与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;

    //...
}