@joincolunm with foreignKey Hibernate

Asked

Viewed 134 times

0

Hello I wanted to make a foreign key, but I don’t understand the syntax.

In the Endereco class:

@Embeddable
@Table(name = "tab_endereco")
public class Endereco {

@Column(length = 40, nullable = false)
private String logradouro;

@Column(length = 40, nullable = false)
private String bairro;

@JoinColumn(columnDefinition = "char(3)", nullable = false, foreignKey = @ForeignKey(name="fk_cidade"))
private Cidade cidade;

In the City class, where we catch the acronym:

@Entity
@Table(name = "tab_cidade")
public class Cidade {


@Id
@JoinColumn(columnDefinition = "char(3)", nullable = false, referencedColumnName = "fk_cidade")
private String sigla;

@Column(length = 40, nullable = false)
private String nome;

@Column(columnDefinition = "char(2)", nullable = false)
private Estado estado;

1 answer

2


Hello, here’s an example of how you can create relationship in your classes.

State-class:

@Entity
public class Estado {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String nome;
private String sigla;

As for the City Class, in this class I took the liberty of putting as primary key a field of Id, I don’t know if I would understand your need, but you can change according to the rules of your project:

@Entity
public class Cidade {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long cidadeId;
private String nome;

@ManyToOne
@JoinColumn(name = "estadoId")
private Estado estado;

Finally the class address, in this class did not put the state field because the City already has a state bound to it:

public class Endereço {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String logradouro;

@ManyToOne
@JoinColumn(name = "cidadeId")
private Cidade cidade;

So come on:

@Id = Specifies the unique identification attribute of the object to be persisted;

@GeneratedValue(strategy=GenerationType.IDENTITY) = When using this annotation SGDB is responsible for generating the ID value of the object incrementally;

@ManyToOne = Sets a cardinality ratio of many to one, ie Many addresses are in a single city for example;

In your code I realized that you had used a note @Embeddable, in my example I removed because this annotation is used when working with composite key.

Thanks.

  • In class Cidade, the attribute estado shouldn’t be @ManyToOne? instead of @OneToMany?

  • You’re right, I made the correction.

  • In this case, @Manytoone is saying that the many Address for a City? And how does the private City identify which attribute of the City class is the one that will serve as the foreign key? Through @Id?

  • And @Joincolumn(name = "cityId"), the name inside Joincolunm has to be the same as the attribute name(private Long cityId) ?

  • Hello Danilo, yes, when you do the mapping to define the relationship between the entities the API itself already makes the relationship to the primary key of the class we are linking, As to use the same name, it is not necessary, I use to keep easier reading.

  • Danilo, has a material that is a summary of some annotations that facilitates the understanding of the use: http://www.techferry.com/articles/hibernate-jpa-annotations.html

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.