【问题标题】:Hibernate mapping field as a primary and foreign keyHibernate 映射字段作为主键和外键
【发布时间】: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 列在哪里?
  • 对不起,我拼错了。状态 = 类型。我修好了

标签: java hibernate


【解决方案1】:

看起来你在这里的情况是客户端有一个复杂的密钥,该密钥部分源自它与类型的关联。

在这种情况下,我认为映射应该如下:

实体类:

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;

    //...
}

【讨论】:

  • 无法找到具有逻辑名称的列:client_own_id in client ;/ 主键中的嵌入 dict 键不应覆盖 client_type、client_own_id 的名称?
  • 这是一个问题、事实陈述、错误信息还是什么?
  • 那是错误。我在客户端和主键中将每次出现的client_own_id更改为client_own_id2,但它仍然在client_own_id上失败。因此,任何实体和客户之间的关系现在都被制动了,因为关系看不到 client_own_id 列
猜你喜欢
  • 1970-01-01
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
  • 2018-06-07
  • 1970-01-01
  • 2013-12-09
相关资源
最近更新 更多