JoinColumn Hibernate / API
JoinColumn Hibernate / API
我遇到了以下问题:我只想加入 table 市场(主要)和电话,但是在使用注释执行此操作时遇到了一些问题。
我只是想使用 Comercio.id 作为来自 Telefone.fk_id 的相应外键,但它无法识别它。我还想问一下是否需要为两者创建一个存储库接口 类。
编辑。此外,对于这样的示例,POST 请求看起来如何?
Comercio.java
@Entity
@Table(name = "comercio")
public class Comercio {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nome;
private String cnpj;
private String endereco;
@OneToMany(targetEntity = Telefone.class , mappedBy = "comercio", fetch = FetchType.LAZY)
private List<Telefone> telefones;
private String email;
Telefone.java
@Entity
@Table(name = "telefone")
public class Telefone {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long telefone_id;
@ManyToOne
@JoinColumn(name="fk_id", referencedColumnName = "id")
private Comercio comercio;
private String telefone;
我得到的答案:
{
"nome" : "Americanas",
"cnpj" : "000",
"endereco" : "SQN 112",
"telefones" : [ ],
"email" : "contato@americanas.com",
"comercio_id" : 1
}
控制台:
Hibernate: select comercio0_.id as id1_0_, comercio0_.cnpj as cnpj2_0_, comercio0_.email as email3_0_, comercio0_.endereco as endereco4_0_, comercio0_.nome as nome5_0_ from comercio comercio0_
Hibernate: select telefones0_.fk_id as fk_id3_1_0_, telefones0_.telefone_id as telefone1_1_0_, telefones0_.telefone_id as telefone1_1_1_, telefones0_.fk_id as fk_id3_1_1_, telefones0_.telefone as telefone2_1_1_ from telefone telefones0_ where telefones0_.fk_id=**?**
感谢任何帮助。
我建议电话本身不是一个实体:您永远不会想要查询电话,它只作为 Comercio 的一部分存在。
它可能是一个 Embeddable
而不是一个实体,但是因为它只有一个 属性 - 数字 - 然后您可以简化为下面并将其映射为字符串集合。那么就不需要 Telefone
class.
@Entity
@Table(name = "comercio")
public class Comercio {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nome;
private String cnpj;
private String endereco;
@ELementCollection
@CollectionTable(name = "comercio_telefone",
joinColumns=@JoinColumn(name="comercio_id"))
private List<String> telefones;
private String email;
}
我遇到了以下问题:我只想加入 table 市场(主要)和电话,但是在使用注释执行此操作时遇到了一些问题。
我只是想使用 Comercio.id 作为来自 Telefone.fk_id 的相应外键,但它无法识别它。我还想问一下是否需要为两者创建一个存储库接口 类。
编辑。此外,对于这样的示例,POST 请求看起来如何?
Comercio.java
@Entity
@Table(name = "comercio")
public class Comercio {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nome;
private String cnpj;
private String endereco;
@OneToMany(targetEntity = Telefone.class , mappedBy = "comercio", fetch = FetchType.LAZY)
private List<Telefone> telefones;
private String email;
Telefone.java
@Entity
@Table(name = "telefone")
public class Telefone {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long telefone_id;
@ManyToOne
@JoinColumn(name="fk_id", referencedColumnName = "id")
private Comercio comercio;
private String telefone;
我得到的答案:
{
"nome" : "Americanas",
"cnpj" : "000",
"endereco" : "SQN 112",
"telefones" : [ ],
"email" : "contato@americanas.com",
"comercio_id" : 1
}
控制台:
Hibernate: select comercio0_.id as id1_0_, comercio0_.cnpj as cnpj2_0_, comercio0_.email as email3_0_, comercio0_.endereco as endereco4_0_, comercio0_.nome as nome5_0_ from comercio comercio0_
Hibernate: select telefones0_.fk_id as fk_id3_1_0_, telefones0_.telefone_id as telefone1_1_0_, telefones0_.telefone_id as telefone1_1_1_, telefones0_.fk_id as fk_id3_1_1_, telefones0_.telefone as telefone2_1_1_ from telefone telefones0_ where telefones0_.fk_id=**?**
感谢任何帮助。
我建议电话本身不是一个实体:您永远不会想要查询电话,它只作为 Comercio 的一部分存在。
它可能是一个 Embeddable
而不是一个实体,但是因为它只有一个 属性 - 数字 - 然后您可以简化为下面并将其映射为字符串集合。那么就不需要 Telefone
class.
@Entity
@Table(name = "comercio")
public class Comercio {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nome;
private String cnpj;
private String endereco;
@ELementCollection
@CollectionTable(name = "comercio_telefone",
joinColumns=@JoinColumn(name="comercio_id"))
private List<String> telefones;
private String email;
}