无法在springboot中设置一对一关系,@joincolumns(column不存在)

mzillmmw  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(266)

我有表,我不会翻译数据库对象,因为orm可能使用命名约定。

  1. casa_venda (in english house_sale, the table name)
  2. id_anunciante (in english id of the announcer, column to join)

校正器

  1. corretor (in english would be broker)
  2. id (in english id, column to join)

一套房子属于一个经纪人。
我的实体类如下所示:

  1. @Entity
  2. @Table(name="casa_venda")
  3. public class CasaVenda extends Casa {
  4. @Column(name="id_anunciante", insertable=false, updatable=false)
  5. private Integer idAnunciante;
  6. @Column(name="preco")
  7. private Integer preco;
  8. public Integer getPreco() {
  9. return preco;
  10. }
  11. public void setPreco(Integer preco) {
  12. this.preco = preco;
  13. }
  14. public Integer getIdAnunciante() {
  15. return idAnunciante;
  16. }
  17. public void setIdAnunciante(Integer idAnunciante) {
  18. this.idAnunciante = idAnunciante;
  19. }
  20. public Corretor getCorretor() {
  21. return corretor;
  22. }
  23. public void setCorretor(Corretor corretor) {
  24. this.corretor = corretor;
  25. }
  26. @OneToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
  27. @JoinTable(name="corretor",
  28. joinColumns={@JoinColumn(name="id_anunciante", referencedColumnName="id")}
  29. )
  30. private Corretor corretor;
  31. }

其他实体

  1. /**
  2. *
  3. * @author diego
  4. */
  5. @Entity
  6. @Table(name="corretor")
  7. public class Corretor {
  8. @Id
  9. @GeneratedValue(strategy=GenerationType.IDENTITY)
  10. @Column(name="id")
  11. private long id;
  12. @Column(name="primeiro_nome")
  13. private String primeiroNome;
  14. @Column(name="sobrenome")
  15. @NotNull(message="O sobrenome é obrigatório")
  16. private String sobrenome;
  17. @Column(name="id_sexo")
  18. private int idSexo;
  19. @Column(name="creci")
  20. @NotNull(message="O creci é obrigatório")
  21. private String creci;
  22. @Column(name="email")
  23. @Pattern(regexp="(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])", message="O email é inválido!")
  24. private String email;
  25. @Column(name="senha")
  26. private String senha;
  27. public long getId() {
  28. return id;
  29. }
  30. public void setId(long id) {
  31. this.id = id;
  32. }
  33. public String getPrimeiroNome() {
  34. return primeiroNome;
  35. }
  36. public void setPrimeiroNome(String primeroNome) {
  37. this.primeiroNome = primeroNome;
  38. }
  39. public String getSobrenome() {
  40. return sobrenome;
  41. }
  42. public void setSobrenome(String sobrenome) {
  43. this.sobrenome = sobrenome;
  44. }
  45. public String getCreci() {
  46. return creci;
  47. }
  48. public void setCreci(String creci) {
  49. this.creci = creci;
  50. }
  51. public String getEmail() {
  52. return email;
  53. }
  54. public void setEmail(String email) {
  55. this.email = email;
  56. }
  57. public String getSenha() {
  58. return senha;
  59. }
  60. public void setSenha(String senha) {
  61. this.senha = senha;
  62. }
  63. public int getIdSexo() {
  64. return idSexo;
  65. }
  66. public void setIdSexo(int idSexo) {
  67. this.idSexo = idSexo;
  68. }
  69. }

当我尝试使用findbyid时,我得到:org.postgresql.util.psqlexception:error:column casavenda0\u 1\u.id\u anunciante does not exists提示:也许您是想引用列“casavenda0\u.id\u anunciante”。

xqkwcwgp

xqkwcwgp1#

我给专栏加了这样的注解,它起了作用:

  1. @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  2. @JoinColumn(name = "id_anunciante", referencedColumnName = "id")
  3. private Corretor corretor;

相关问题