我尝试让hibernate 4运行(随JBoss AS 7一起提供)并将我的应用程序部署为EAR(persistence.xml iis在EAR的META-INF中)。 Hibernate好像在运行。我有两个班级,只要我不在另一个班级内使用一个班级就不会抱怨。
我有一个会话:
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) { [...] }
}
我有一个用户
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) { [...] }
}
如果我启动服务器,它会抱怨:
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)
如果我从会话类中删除public User user;
,则不会抱怨。
这两个类都列在persistence.xml
中,我尝试过:
我还能检查什么?可能是什么问题?
答案 0 :(得分:2)
将@ManyToOne
注释添加到会话类中的User成员中(假设单个用户可以在您的数据库中有多个会话...如果用户只有一个现有会话,则可以使用{{1 }}
答案 1 :(得分:1)
由于您没有为属性User
提供任何映射,因此出现错误。请尝试按以下方式修改代码。
@OneToOne
public User user
有关使用注释在hibernate中@OneToOne
映射的示例,请参阅here。