【问题标题】:Solve hibernate error: Repeated column in mapping for entity?解决休眠错误:实体映射中的重复列?
【发布时间】:2018-05-25 11:51:54
【问题描述】:

我的表 ctl 与表有两个关系:psr 和 psg。 此表具有相同的 PK 名称:COD_FILEFE_S

public class Psr{
  @Id
  @Column(name = "COD_FILEFE_S")
  private BigDecimal codFilefeS;
}

public class Psg{
  @Id
  @Column(name = "COD_FILEFE_S")
  private BigDecimal codFilefeS;
}

可以用 hibernate 映射这个吗? 使用以下模型,我有 org.hibernate.MappingException:实体映射中的重复列:

public class Ctl{

/** The cod filefe s. */
@ManyToOne(targetEntity = Psg.class)
@JoinColumn(name = "COD_FILEFE_S", referencedColumnName = "COD_FILEFE_S", nullable = false)
private BigDecimal codFilefeS;

/** The cod filefe s. */
@ManyToOne(targetEntity = Psr.class)
@JoinColumn(name = "COD_FILEFE_S", referencedColumnName = "COD_FILEFE_S", nullable = false)
private BigDecimal codFilefeSPert;

}

【问题讨论】:

  • 正确格式化您的代码。不可读。

标签: java spring hibernate jakarta-ee


【解决方案1】:

正如您提到的两列相同的名称,您会收到此错误.. 更改第二个的列名..

public class ctl{

/** The cod filefe s. */
@ManyToOne(targetEntity = Psg.class)
@JoinColumn(name = "COD_FILEFE_S", referencedColumnName = "COD_FILEFE_S", nullable = false)
private BigDecimal codFilefeS;

/** The cod filefe s. */
@ManyToOne(targetEntity = Psr.class)
@JoinColumn(name = "COD_FILEFE_S_PERT", referencedColumnName = "COD_FILEFE_S", nullable = false)
private BigDecimal codFilefeSPert;

}

【讨论】:

  • 是的,这是简单的解决方案,但它不可能同名?
  • 是的,绝对不可能..你怎么能在一个表中有两个同名的列??
  • 它的两个不同的表psg和psr,看看targetEntity
  • 我同意,两个 targetEntities 是不同的。但是你在这个类中映射这两个列..所以在内部这将创建另一个包含两个列的表,引用两个 targetEntity 表..
猜你喜欢
  • 2011-05-13
  • 1970-01-01
  • 2016-11-20
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 2014-06-20
  • 1970-01-01
相关资源
最近更新 更多