2013-03-20 51 views
1

我正在抓我的頭不知道爲什麼這個例外不斷彈出每當我試圖用彈簧的新用戶保存到我的數據庫和休眠用戶對象上未知的實體。org.hibernate.MappingException:包含Entity註解

我有雙重檢查並確保@Entity註釋存在,甚至添加@Table annoation說這個對象所屬的表,我仍然recieving此錯誤

下面是我的用戶對象:

@Entity 
@Table(name = "USER") 
public class User implements Serializable { 

    /** 
    * Serial version 
    */ 
    private static final long serialVersionUID = 1L; 

    @JsonElement(type = JsonElementType.JSON_KEY) 
    public static final String KEY_BIO = "Bio"; 

    @JsonElement(type = JsonElementType.JSON_KEY) 
    public static final String SURNAME = "Surname"; 

    @JsonElement(type = JsonElementType.JSON_KEY) 
    public static final String KEY_FIRST_NAME = "FirstName"; 

    @JsonElement(type = JsonElementType.JSON_KEY) 
    public static final String KEY_PASSWORD = "Password"; 

    @JsonElement(type = JsonElementType.JSON_KEY) 
    public static final String KEY_EMAIL = "EmailAddress"; 

    @JsonElement(type = JsonElementType.JSON_KEY) 
    public static final String KEY_USERNAME = "Username"; 

    @JsonElement(type = JsonElementType.JSON_KEY) 
    public static final String KEY_USER_ID = "UserId"; 

    @JsonElement(type = JsonElementType.JSON_KEY) 
    public static final int USERNAME_LENGTH = 5; 

    @Id 
    @Column(name = "USER_ID") 
    @NotNull 
    private String id; 

    @Column(name = "USERNAME", nullable = false, unique = true) 
    @NotNull 
    private String username; 

    @Column(name = "EMAIL_ADDRESS", nullable = false, unique = true) 
    @NotNull 
    private String emailAddress; 

    @Column(name = "PASSWORD") 
    @NotNull 
    @Size(min = 5, max = 25) 
    private String password; 

    @Column(name = "FIRST_NAME") 
    private String firstName; 

    @Column(name = "SURNAME") 
    private String surname; 

    @Column(name = "COUNTRY") 
    private String country; 

    @Column(name = "BIO") 
    private String bio; 



    @Column(name = "SESSION_ID") 
    private String sessionId; 

    @Column(name = "LAST_REST_CALL_MADE") 
    private Date lastRestCallMade; 

    @Column(name = "STATUS") 
    @NotNull 
    private int status; 

    @OneToOne 
    @JoinColumn(name = "imageId") 
    private ProfileImage profileImage; 

    public User() { 
     id = UUID.randomUUID().toString(); 
    } 

    @JsonElement(type = JsonElementType.GETTER) 
    public ProfileImage getProfileImage() { 
     return profileImage; 
    } 

    @JsonElement(type = JsonElementType.SETTER) 
    public void setProfileImage(ProfileImage profileImage) { 
     this.profileImage = profileImage; 
    } 

    @JsonElement(type = JsonElementType.GETTER) 
    public String getId() { 
     return id; 
    } 

    @JsonElement(type = JsonElementType.SETTER) 
    public void setId(String id) { 
     this.id = id; 
    } 

    @JsonElement(type = JsonElementType.GETTER) 
    public String getUsername() { 
     return username; 
    } 

    @JsonElement(type = JsonElementType.SETTER) 
    public void setUsername(String username) { 
     this.username = username; 
    } 

    @JsonElement(type = JsonElementType.GETTER) 
    public String getEmailAddress() { 
     return emailAddress; 
    } 

    @JsonElement(type = JsonElementType.SETTER) 
    public void setEmailAddress(String emailAddress) { 
     this.emailAddress = emailAddress; 
    } 

    @JsonElement(type = JsonElementType.GETTER) 
    public String getPassword() { 
     return password; 
    } 

    @JsonElement(type = JsonElementType.SETTER) 
    public void setPassword(String password) { 
     this.password = password; 
    } 

    @JsonElement(type = JsonElementType.GETTER) 
    public String getFirstName() { 
     return firstName; 
    } 

    @JsonElement(type = JsonElementType.SETTER) 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    @JsonElement(type = JsonElementType.GETTER) 
    public String getSurname() { 
     return surname; 
    } 

    @JsonElement(type = JsonElementType.SETTER) 
    public void setSurname(String surname) { 
     this.surname = surname; 
    } 

    @JsonElement(type = JsonElementType.GETTER) 
    public String getBio() { 
     return bio; 
    } 

    @JsonElement(type = JsonElementType.SETTER) 
    public void setBio(String bio) { 
     this.bio = bio; 
    } 

    @JsonElement(type = JsonElementType.GETTER) 
    public String getCountry() { 
     return country; 
    } 

    @JsonElement(type = JsonElementType.SETTER) 
    public void setCountry(String country) { 
     this.country = country; 
    } 

    /** 
    * @return the sessionId 
    */ 
    public String getSessionId() { 
     return sessionId; 
    } 

    /** 
    * @param sessionId the sessionId to set 
    */ 
    @JsonElement(type = JsonElementType.SETTER) 
    public void setSessionId(String sessionId) { 
     this.sessionId = sessionId; 
    } 

    /** 
    * @return the lastRestCallMade 
    */ 
    public Date getLastRestCallMade() { 
     return lastRestCallMade; 
    } 

    /** 
    * @param lastRestCallMade the lastRestCallMade to set 
    */ 
    public void setLastRestCallMade(Date lastRestCallMade) { 
     this.lastRestCallMade = lastRestCallMade; 
    } 

    /** 
    * @return the status 
    */ 
    public int getStatus() { 
     return status; 
    } 

    /** 
    * @param status the status to set 
    */ 
    public void setStatus(int status) { 
     this.status = status; 
    } 

} 

這裏是我的userDAO的呢

@Override 
    public void addNewUser(User user) throws InvalidDataException { 
     // TODO: Add Transaction manager object here either using annototation 
     // or creating it programatically 

     if (mSessionFactory != null) { 
      session = mSessionFactory.getCurrentSession(); 
      session.beginTransaction(); 
      if (user.getUsername() == null 
        || user.getUsername().length() < User.USERNAME_LENGTH) { 
       throw new InvalidDataException(
         InvalidDataException.ERROR_USERNAME_NULL); 
      } 

      session.save(user); 

     }else{ 
      throw new RuntimeException("mSession is null"); 
     } 
    } 

非常簡單SUFF這裏。

下面

充分stracktrace:

org.hibernate.MappingException: Unknown entity: com.jr.freedom.user.User 
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1092) 
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1399) 
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204) 
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189) 
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) 
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:753) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:745) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:741) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352) 
    at $Proxy20.save(Unknown Source) 
    at com.jr.freedom.dao.UserDao.addNewUser(UserDao.java:50) 
    at com.jr.freedom.user.UserService.registerNewUser(UserService.java:47) 
    at com.jr.freedom.controllers.UserController.createNewAccount(UserController.java:57) 
    at com.test.jr.freedom.controllers.UserControllerTest.testCreateNewAccount(UserControllerTest.java:61) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

它,當我調用session.save(用戶)崩潰;

+1

請確保您使用的是@ javax.persistence.Entity而不是'@org.hibernate.annotations.Entity'。請參閱[Hibernate註釋](http://stackoverflow.com/q/7723878/851811)。 – 2013-03-20 16:19:22

+0

發佈會話工廠XML bean定義。 – xyz 2013-03-20 16:20:59

+0

賓果我們有一個贏家(sessionFactory xml是問題,我會發布爲什麼,並希望看看是否有更好的更容易的修復程序 – jonney 2013-03-20 16:30:27

回答

2

賓果。這裏是位於我的XML配置文件,它配置了一個SessionFactory的問題:

<property name="annotatedClasses"> 
      <list> 
       <value>com.jr.freedom.user.User</value> 
       <value>com.jr.freedom.user.ProfileImage</value> 
      </list> 
     </property> 

那失蹤下面這個bean:

<bean id="mySessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="configLocation" value="hibernate.cfg.xml" /> 
     <property name="annotatedClasses"> 
      <list> 
       <value>com.jr.freedom.user.User</value> 
       <value>com.jr.freedom.user.ProfileImage</value> 
      </list> 
     </property> 

    </bean> 

所以我補充說,它的工作!問題是,如果我必須維護和使用100個實體,該怎麼辦?這將是一個雜事,在sessionFactory內部創建一長串annotatedClasses值!任何想法如何我可以告訴春季或冬眠說「請掃描選定的軟件包,並找到並使用我的實體標記對象」。

+0

感謝您的答案以及我要求您將此答案標記爲**接受** 。 – 2014-02-24 05:58:04

0

您可以讓任何JPA提供程序掃描jar,但是您需要將所有實體類打包到單獨的jar中。任何其他解決方案都是特定於提供商的攻擊。我通過使用google反射來掃描註釋,然後使用Hibernate的EjbConfiguration類來註冊所有實體註釋類;但是已經棄用休眠4和即將消失在Hibernate中5

0

我忘了我的hbm.xml添加到我cfg.xml文件。一旦我添加它,這個例外就消失了。

<mapping resource="com/swipex/backend/hibernate/CServiceCenters.hbm.xml" /> 
相關問題