Reputation: 146
I am trying make a composite-key at JPA, which one of this keys is a class and the another is an Enumeration.
But it is giving the following error:
Caused by: org.hibernate.MappingException: Could not determine type for: br.com.model.Restaurante, at table: ImagemRestaurante, for columns: [org.hibernate.mapping.Column(restaurante)]
ImagemRestaurante.java
@Entity
@IdClass(ImagemRestauranteId.class)
public class ImagemRestaurante implements Serializable {
private static final long serialVersionUID = -4650712996667906167L;
@Id
@JoinColumn(name="idRestaurante")
private Restaurante restaurante;
@Id
@Enumerated(EnumType.ORDINAL)
private TipoImagemRestaurante tipoImagemRestaurante;
private String nome;
private String formato;
private Long tamanho;
private Integer altura;
private Integer largura;
@Transient
private String caminho;
@Transient
private String caminhoHTML;
public ImagemRestaurante() {
}
// Get Sets
}
ImagemRestauranteId.java
public class ImagemRestauranteId implements Serializable {
private static final long serialVersionUID = -6216145537667024048L;
private Restaurante restaurante;
private TipoImagemRestaurante tipoImagemRestaurante;
public ImagemRestauranteId() {
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if ( ! (obj instanceof ImagemRestauranteId ) ) {
return false;
}
ImagemRestauranteId imaId = (ImagemRestauranteId) obj;
return (imaId.restaurante == restaurante) &&
(imaId.tipoImagemRestaurante == tipoImagemRestaurante);
}
@Override
public int hashCode() {
return restaurante.hashCode() + tipoImagemRestaurante.hashCode();
}
// Get Sets
}
Restaurante.java
@Entity
public class Restaurante {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long idRestaurante;
private String nmRestaurante;
private String dsRestaurante;
@OneToOne
private Telefone telefone;
@OneToOne
private Endereco endereco;
private Boolean permitirComentario;
private Boolean permitirNota;
@ManyToOne
private Usuario usuario;
public Restaurante() {
}
// Get Sets
}
Upvotes: 1
Views: 137
Reputation: 3956
Add @ManyToOne
mapping to relationship with Restaurante
@Id
@ManyToOne
@JoinColumn(name="idRestaurante")
private Restaurante restaurante;
Existence of ImagemRestaurante instance depends on existance of Restaurante instance. That's why Restaurante is called parent in relation to ImagemRestaurante by the JPA spec
If an Id attribute in the entity is a many-to-one or one-to-one relationship to a parent entity, the corresponding attribute in the id class must be of the same Java type as the id class or embedded id of the parent entity (if the parent entity has a composite primary key) or the type of the Id attribute of the parent entity (if the parent entity has a simple primary key).
private Restaurante restaurante;
to private Long restaurante;
in ImagemRestauranteId
class. The ImagemRestauranteId being an IdClass has to hold only the PK reference to Restaurante. Following words from JPA spec applyA simple primary key or a field or property of a composite primary key should be one of the following types: any Java primitive type; any primitive wrapper type; java.lang.String; java.util.Date; java.sql.Date; java.math.BigDecimal; java.math.BigInteger.
Upvotes: 1