如何在org.hibernate.annotations.Formula中使用子表列

huangapple go评论119阅读模式
英文:

How to use child table column in org.hibernate.annotations.Formula

问题

  1. 我需要使用 `@Formula` 来连接父表和子表的某些列
  2. 以下是实体类的示例代码
  3. @Entity
  4. @Table(name = "parent1")
  5. public class Parent1 implements Serializable {
  6. @Id
  7. private BigInteger id;
  8. @Column(name = "childId")
  9. private BigInteger childId;
  10. @Column(name = "col1")
  11. private String col1;
  12. @Column(name = "col2")
  13. private String col2;
  14. @Formula("CONCAT_WS(' ', (SELECT p2.child_colm FROM parent2 p2 WHERE p2.childId = childId), col1, col2)")
  15. private String combinedName;
  16. @OneToOne
  17. @JoinColumn(name = "childId")
  18. private Parent2 parent2;
  19. }
  20. @Entity
  21. @Table(name = "parent2")
  22. public class Parent2 implements Serializable {
  23. @Id
  24. @Column(name = "childId")
  25. private BigInteger childId;
  26. @Column(name = "child_colm")
  27. private String child_colm;
  28. }

在这样的情况下,会返回 Unknown column 'Parent2.child_colm' 的错误。

英文:

I need to concat some columns from both parent and child table using @Formula
Here is the Entities

  1. @Entity
  2. @Table(name = "parent1")
  3. public class Parent1 implements Serializable {
  4. @Id
  5. private BigInteger id;
  6. @Column(name = "childId")
  7. private BigInteger childId;
  8. @Column(name = "col1")
  9. private String col1;
  10. @Column(name = "col2")
  11. private String col2;
  12. @Formula("CONCAT_WS(' ',Parent2.child_colm,col1,col2)")
  13. private String combinedName;
  14. @OneToOne
  15. @JoinColumn(name = "childId")
  16. private Parent2 parent2;
  17. }
  18. @Entity
  19. @Table(name = "parent2")
  20. public class Parent2 implements Serializable {
  21. @Id
  22. @Column(name = "childId")
  23. private BigInteger childId;
  24. @Column(name = "child_colm")
  25. private String child_colm;
  26. }

While giving like this it returns Unknown column 'Parent2.child_colm'

答案1

得分: 1

我建议您不要在这里使用 @Formula,而只需编写以下方法:

  1. import javax.persistence.Transient;
  2. @Entity
  3. @Table(name = "parent1")
  4. public class Parent1 implements Serializable {
  5. @Transient
  6. public String getCombinedName() {
  7. return Stream.of(parent2.child_colm, col1, col2)
  8. .filter(s -> s != null && !s.isEmpty())
  9. .collect(Collectors.joining(" "));
  10. }
  11. }

@Transient 注释 用于指定不应持久化给定的实体属性。

英文:

I would suggest you instead of using @Formula here just write the following method:

  1. import javax.persistence.Transient;
  2. @Entity
  3. @Table(name = "parent1")
  4. public class Parent1 implements Serializable {
  5. @Transient
  6. public String getCombinedName() {
  7. return Stream.of(parent2.child_colm, col1, col2)
  8. .filter(s -> s != null && !s.isEmpty())
  9. .collect(Collectors.joining(" "));
  10. }
  11. }

The @Transient annotation is used to specify that a given entity attribute should not be persisted.

huangapple
  • 本文由 发表于 2020年10月15日 19:19:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64370438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定