I try to get hibernate 4 running (shipped with JBoss AS 7) and deploy my application as EAR (persistence.xml iis in the META-INF of the EAR). Hibernate seems to run. I have two classes and is does not complain as long as I do not use one class inside the other.
I have a session:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "sessions")
public class Session {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
public Long dbid;
public String id;
public User user; // This is problematic!
public Session() {
}
public Session(User user) {
this.id = generateID();
this.user = user;
}
private String generateID() {
return Long.toString((long) (Math.random() * 1000000000.0));
}
@Override
public int hashCode() { [...] }
@Override
public boolean equals(Object obj) { [...] }
}
And I have a user
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
public Long dbid;
public String name;
public String password;
public User() {
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
@Override
public int hashCode() { [...] }
@Override
public boolean equals(Object obj) { [...] }
}
If I start the server it complains:
Caused by: org.hibernate.MappingException: Could not determine type for: myproject.model.User, at table: sessions, for columns: [org.hibernate.mapping.Column(user)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:303)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:287)
at org.hibernate.mapping.Property.isValid(Property.java:215)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:466)
at org.hibernate.mapping.RootClass.validate(RootClass.java:267)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1285)
It does not complain if I remove public User user; from the session class.
Both classes are listed in the persistence.xml and I have tried:
- generated getters/setters and place the annotations at the getters
- generate hashCode and equals appropriately
What could I check additionally? What might be the problem?
Add the
@ManyToOneannotation to the User member in your session class (assuming a single user can have multiple sessions in your database … if a user only ever has one extant session, you can use@OneToOne