gpt4 book ai didi

java - Spring Boot中无法建立一对一关系,@JoinColumns(列不存在)

转载 作者:行者123 更新时间:2023-12-01 16:20:49 25 4
gpt4 key购买 nike

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

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

校正器

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

一所房子属于一位经纪人。

我的实体类是这样的:

@Entity
@Table(name="casa_venda")
public class CasaVenda extends Casa {

@Column(name="id_anunciante", insertable=false, updatable=false)
private Integer idAnunciante;


@Column(name="preco")
private Integer preco;


public Integer getPreco() {
return preco;
}

public void setPreco(Integer preco) {
this.preco = preco;
}

public Integer getIdAnunciante() {
return idAnunciante;
}

public void setIdAnunciante(Integer idAnunciante) {
this.idAnunciante = idAnunciante;
}

public Corretor getCorretor() {
return corretor;
}

public void setCorretor(Corretor corretor) {
this.corretor = corretor;
}





@OneToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name="corretor",
joinColumns={@JoinColumn(name="id_anunciante", referencedColumnName="id")}
)

private Corretor corretor;

}

其他实体

      /**
*
* @author diego
*/
@Entity
@Table(name="corretor")
public class Corretor {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private long id;

@Column(name="primeiro_nome")

private String primeiroNome;

@Column(name="sobrenome")
@NotNull(message="O sobrenome é obrigatório")
private String sobrenome;

@Column(name="id_sexo")
private int idSexo;


@Column(name="creci")
@NotNull(message="O creci é obrigatório")
private String creci;

@Column(name="email")
@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!")
private String email;

@Column(name="senha")
private String senha;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getPrimeiroNome() {
return primeiroNome;
}

public void setPrimeiroNome(String primeroNome) {
this.primeiroNome = primeroNome;
}

public String getSobrenome() {
return sobrenome;
}

public void setSobrenome(String sobrenome) {
this.sobrenome = sobrenome;
}

public String getCreci() {
return creci;
}

public void setCreci(String creci) {
this.creci = creci;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getSenha() {
return senha;
}

public void setSenha(String senha) {
this.senha = senha;
}

public int getIdSexo() {
return idSexo;
}

public void setIdSexo(int idSexo) {
this.idSexo = idSexo;
}


}

当我尝试使用 findById 时,我得到:org.postgresql.util.PSQLException:错误:列 casavenda0_1_.id_anunciante 不存在 提示:也许您想引用“casavenda0_.id_anunciante”列。

最佳答案

我像这样注释了该列并且它起作用了:

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

关于java - Spring Boot中无法建立一对一关系,@JoinColumns(列不存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62287740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com