2011-11-18 115 views
1

我嘗試讓hibernate 4運行(隨JBoss AS 7一起提供)並將我的應用程序部署爲EAR(persistence.xml iis,位於EAR的META-INF中)。 Hibernate似乎運行。我有兩個班級,只要我不在另一個班級中使用一個班級,就不會抱怨。如何解決org.hibernate.MappingException?

我有一個會話:

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上市,我曾嘗試:

  1. 生成的getter/setter方法,並把註釋在干將
  2. 產生的hashCode並適當等於

我該怎麼辦另外檢查?可能是什麼問題?

回答

2

@ManyToOne註釋添加至用戶成員在會話類(假設一個用戶可以在你的數據庫多個會話......如果一個用戶永遠只能有一個現存的會話,你可以使用@OneToOne

+0

當然,謝謝。出於某種原因,我的大腦告訴我「OneToOne」是隱含的。 *哎呀* – Arne

1

它因爲你還沒有爲屬性User。嘗試修改代碼,如下所提供的任何映射給了一個錯誤。

@OneToOne 
public User user 

here了有關使用註釋這裏休眠的@OneToOne映射例子。