Hibernate mapping with JPA-Annotations: Could not determine type
I recently started my first Java project that uses Hibernate (and JPA Annotations) for persistence.
I've got the following classes:
User class
*some imports*
@Entity
public class Owner {
private int id;
private String name;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="id")
private Pet pet;
@Id
@GeneratedValue
public int开发者_运维问答 getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setPet(Pet pet) {
this.pet = pet;
}
public Pet getPet() {
return pet;
}
}
Pet Class
*some imports*
@Entity
public class Pet {
private String name;
@Id
@GeneratedValue
private int id;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Whenever I try to save an object, Hibernate gives me the following error output.
Exception in thread "main" org.hibernate.MappingException: Could not determine type for: Pet, at table: Owner, for columns: [org.hibernate.mapping.Column(pet)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:306)
at org.hibernate.mapping.Column.getSqlTypeCode(Column.java:164)
What am I missing? I can't figure out how to do this PF-FK mapping.
Do I need to tell Hibernate which table Pet maps into or is it smart enough to do that? Could someone please suggest how I can change my declaration of the Pet ivar so that Hibernate and I can be happy? :)
Thanks!
I guess it's caused by the fact that you mixed annotations on fields and properties. You need to place annotations in a consistent way, unless you explicitly indicate how they are placed with @Access
/@AccessType
.
When it says it doesn't know the entity, that generally means it has either not found it (if you are using scanning) or you forgot to list it. If you are using JPA, generally you will list the entities in persistence.xml (which is in the META-INF folder).
精彩评论