【问题标题】:Saving value in Hibernate to another table than entity table将 Hibernate 中的值保存到另一个表而不是实体表
【发布时间】:2017-10-12 08:12:32
【问题描述】:

我有两张桌子bo_operatorhist_bo_operator_password。在bo_operator 中,id 列是hist_bo_operator_password 的外键,我可以在hist_bo_operator_password 中拥有许多相同的operator_id,而在bo_operator 中只有一个id

我的实体:

@Entity
@Table(name="bo_operator")
public class Operator implements Serializable

这就是我从hist_bo_operator_password 获取值的方式:

@ElementCollection
@CollectionTable(name="hist_bo_operator_password", joinColumns=@JoinColumn(name="id_operator"))
@Column(name="password")
public List<String> oldPasswords = new ArrayList<String>();

但是当我试图通过以下方式获得一个值时:

@ElementCollection
@CollectionTable(name="hist_bo_operator_password", joinColumns=@JoinColumn(name="id_operator"))
@Column(name="password")
public String oldPassword;

我遇到了错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: local.vlex.operator.model.Operator.oldPassword

我想要做的就是插入hist_bo_operator_password by operator.setOldPassword(oldPassword);。我认为问题在于,如果同一个id 有多个值,它不知道使用哪个密码。 如何实现?

@编辑 我也试过了:

@Table(name="bo_operator")
@SecondaryTable(name = "hist_bo_operator_password",pkJoinColumns=@PrimaryKeyJoinColumn(name="id_operator", referencedColumnName="id"))

我什至找到了 ORDER BY:

@Column(name="password", table="hist_bo_operator_password")
@OrderBy("data_ins")
public String oldPassword;

但在 JPA 中似乎没有 @Limit 或类似的东西,我仍然有许多相同 id 的值导致错误:

org.hibernate.HibernateException: Duplicate identifier in table for: [local.vlex.operator.model.Operator#1]

【问题讨论】:

  • @ElementCollection 用于收集,不用于单值类型。顺便说一句,为什么您只想获得一个值(当您使用集合时)?您只需将oldPassword 添加到您的列表中即可插入!我不知道我是否明白你的想法?
  • 一般来说,我想要@Andriy Slobodyanyk 写的东西,所以接受了答案。保存了整个列表,我只想获取一个(最早的)值并用它进行插入,但没关系,我在表上触发了,当新值出现时,最早的值被删除。

标签: postgresql hibernate spring-boot


【解决方案1】:

正如你在第一句话中描述的那样,你有一对多的关系

@ElementCollection
@CollectionTable(name="hist_bo_operator_password", joinColumns=@JoinColumn(name="id_operator"))
@Column(name="password")
@OrderBy("data_ins")
public List<String> oldPasswords = new ArrayList<String>();

然后添加必要的getter

Optional<String> getPassword() {
    return oldPasswords.size() > 0
        ? Optional.of(oldPasswords.get(0))
        : Optional.empty();
}

二传手

void setPassword(String password) { // or maybe addPassword?
    oldPasswords.add(password);
}

【讨论】:

  • 好吧,这就是我获取最后一个密码的方式,但我也会设置这个 oldPassword 以便稍后在 Operator 对象上使用 save 。目标是插入hist_bo_operator_password,此代码将保存整个列表。
  • @Michu93,好吧,添加setter,修改上面的代码
  • 这与我的预期很接近,但它保存了整个列表,而不仅仅是一个值。我认为需要对 oldPassword 进行一些限制或注释。现在我们正在尝试做清单上的所有事情。
【解决方案2】:

你为什么不创建 hist_bo_operator_password 的实体? 然后,您可以拥有该实体的列表(而不仅仅是字符串列表),只需将另一个对象添加到列表并保存实体运算符。

【讨论】:

  • 我只需要来自hist_bo_operator_password 的一列,所以我认为不需要新的@Entity
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多