2016-05-31 57 views
0

試圖首次從頭開始使用Hibernate。 我有實體類:休眠嘗試將字段值設置爲對象

package centaurus.domain; 

import javax.persistence.*; 

@Entity 
@Table(name="users") 
public class Player { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name="USER_ID") 
    private int id; 

    @Column(name="email") 
    private String email; 

    public Player(){}; 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public int getId() { 
     return id; 
    } 
} 

DAOimpl: 包centaurus.service;

import centaurus.domain.Player; 

import restx.factory.Component; 

import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.SessionFactory; 

@Component 
public class PlayerDAOimpl implements PlayerDAO{ 
    private static SessionFactory factory; 

    public PlayerDAOimpl() { 
     try{ 
      factory = new Configuration(). 
        configure().addAnnotatedClass(Player.class). 
        buildSessionFactory(); 
      // 
     }catch (Throwable ex) { 
      System.err.println("Failed to create sessionFactory object." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 

    } 

    public Player savePlayer(Player player){ 
     Session session = factory.openSession(); 
     Transaction tx = null; 
     Integer playerID = null; 


     try{ 
      tx = session.beginTransaction(); 

      playerID = (Integer) session.save(player); 
      tx.commit(); 

     }catch (HibernateException e) { 
      if (tx!=null) tx.rollback(); 
      e.printStackTrace(); 
     }finally { 
      session.close(); 
     } 
     return player; 
    } 

    public Player getPlayer(Integer playerId){ 
     Session session = factory.openSession(); 
     try{ 
      Player player = (Player)session.get(Player.class, playerId); 
      return player; 
     }catch (HibernateException e) { 

     }finally { 
      session.close(); 
     } 
     return null; 
    } 
} 

來演示錯誤我讓它在startupscript上添加一個播放器。

和錯誤:

-- RESTX >> LOAD ON REQUEST << >> DEV MODE << >> AUTO COMPILE << 
-- for admin console, 
-- VISIT http://127.0.0.1:9091/api/@/ui/ 
-- 

2016-05-31 21:42:48,901 [main   ] [   ] INFO restx.Apps - can't enable Apidocs doclet: make sure tools.jar is in your classpath 
2016-05-31 21:42:49,978 [pool-1-thread-1 ] [   ] INFO restx.classloader.CompilationManager - compilation finished: 12 sources compiled in 1.015 s 
2016-05-31 21:42:50,061 [main   ] [   ] INFO restx.Apps - can't enable Apidocs doclet: make sure tools.jar is in your classpath 
2016-05-31 21:42:50,078 [main   ] [   ] INFO restx.classloader.CompilationManager - watching for changes in [src/main/java, src/main/resources]; current location is /home/arthur/elorhia/api/. 
2016-05-31 21:42:50,145 [main   ] [   ] INFO org.hibernate.Version - HHH000412: Hibernate Core {5.1.0.Final} 
2016-05-31 21:42:50,146 [main   ] [   ] INFO org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found 
2016-05-31 21:42:50,147 [main   ] [   ] INFO org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist 
2016-05-31 21:42:50,802 [main   ] [   ] INFO o.h.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 
2016-05-31 21:42:50,851 [main   ] [   ] WARN org.hibernate.orm.connections - HHH10001002: Using Hibernate built-in connection pool (not for production use!) 
2016-05-31 21:42:50,856 [main   ] [   ] INFO org.hibernate.orm.connections - HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/andromeda] 
2016-05-31 21:42:50,857 [main   ] [   ] INFO org.hibernate.orm.connections - HHH10001001: Connection properties: {user=api, password=****} 
2016-05-31 21:42:50,858 [main   ] [   ] INFO org.hibernate.orm.connections - HHH10001003: Autocommit mode: false 
2016-05-31 21:42:50,860 [main   ] [   ] INFO o.h.e.j.c.i.DriverManagerConnectionProviderImpl - HHH000115: Hibernate connection pool size: 1 (min=1) 
2016-05-31 21:42:51,090 [main   ] [   ] INFO org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 
2016-05-31 21:42:51,321 [main   ] [   ] INFO o.h.validator.internal.util.Version - HV000001: Hibernate Validator 5.0.1.Final 
START SCRIPT! 
org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.String centaurus.domain.Player.email] by reflection for persistent property [centaurus.domain.Player#email] : [email protected] 
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:43) 
    at org.hibernate.property.access.spi.GetterFieldImpl.getForInsert(GetterFieldImpl.java:58) 
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValuesToInsert(AbstractEntityTuplizer.java:521) 
    at org.hibernate.tuple.entity.PojoEntityTuplizer.getPropertyValuesToInsert(PojoEntityTuplizer.java:228) 
    at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValuesToInsert(AbstractEntityPersister.java:4701) 
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:254) 
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182) 
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:113) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) 
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) 
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) 
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:682) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:674) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:669) 
    at centaurus.service.PlayerDAOimpl.savePlayer(PlayerDAOimpl.java:39) 
    at centaurus.Dbmaintain.start(Dbmaintain.java:26) 
    at restx.factory.Factory.start(Factory.java:846) 
    at restx.RestxMainRouterFactory.build(RestxMainRouterFactory.java:450) 
    at restx.RestxMainRouterFactory.newInstance(RestxMainRouterFactory.java:70) 
    at restx.servlet.RestxMainRouterServlet.init(RestxMainRouterServlet.java:74) 
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:519) 
    at org.eclipse.jetty.servlet.ServletHolder.doStart(ServletHolder.java:331) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) 
    at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:747) 
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:265) 
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250) 
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:706) 
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:492) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) 
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) 
    at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) 
    at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95) 
    at org.eclipse.jetty.server.Server.doStart(Server.java:277) 
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64) 
    at restx.server.JettyWebServer.start(JettyWebServer.java:109) 
    at restx.server.JettyWebServer.startAndAwait(JettyWebServer.java:114) 
    at centaurus.AppServer.main(AppServer.java:30) 
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field centaurus.domain.Player.email to centaurus.domain.Player 
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) 
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) 
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58) 
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36) 
    at java.lang.reflect.Field.get(Field.java:393) 
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:39) 
    ... 40 more 
2016-05-31 21:42:51,673 [main   ] [   ] INFO restx.monitor.MetricsConfiguration - registering Metrics JVM metrics 
2016-05-31 21:44:41,646 [pool-2-thread-1 ] [   ] INFO restx.classloader.CompilationManager - compilation finished: 1 sources compiled in 86.94 ms 

它看起來像它試圖將電子郵件字段設置爲玩家對象。 我無法弄清楚爲什麼!,我做錯了什麼?

編輯:根據要求我添加的代碼創建播放器和調用DAO

package centaurus; 

import centaurus.domain.Player; 
import centaurus.service.PlayerDAO; 
import centaurus.service.PlayerDAOimpl; 
import restx.factory.AutoStartable; 
import restx.factory.Component; 

import javax.inject.Named; 


@Component 
public class Dbmaintain implements AutoStartable{ 
    private PlayerDAO playerDAO; 

    public Dbmaintain(@Named("PlayerDAOimpl") PlayerDAO playerDAO) { 
     this.playerDAO = playerDAO; 
    } 

    public void start(){ 
     System.out.println("START SCRIPT!"); 

     //test 
     Player p = new Player(); 
     p.setEmail("test"); 
     playerDAO.savePlayer(p); 

     try { 
     // URL configurationUrl = new File("dbmaintain.properties").toURI().toURL(); 
      // MainFactory mainFactory = new MainFactory(configurationUrl); 
      // DbMaintainer dbMaintainer = mainFactory.createDbMaintainer(); 
      // dbMaintainer.updateDatabase(false); 
     } catch(Exception e) { 

     } 
    } 
} 

編輯添加git的鏈接,如果人們想其他位: https://github.com/ArthurGibbs/Centaurus-

+0

請顯示代碼填充播放器對象的位置。 –

+0

明智的提示:'SessionFactory'是一個昂貴的對象。你不應該爲每個DAO實現創建它。相反,定義一個'HibernateUtil'類並在那裏定義'SessionFacory'的一個靜態對象,並在每次需要時使用該對象。 –

+0

我如何繞過addAnnotatedClass,使用hibernateUtil類,我發現的唯一教程是要麼有bean映射,要麼在工廠中指定類 – Arthur

回答

0

你是如何設置/創建Player

Caused by: java.lang.IllegalArgumentException: Can not set java.lang.String field centaurus.domain.Player.email to centaurus.domain.Player 

這是說,有是centaurus.domain.Player類的對象存儲到一個字符串領域的嘗試。獲得進一步的說明,附上正在修改澈實際代碼/ perfore它傳遞給DAO

+0

感謝您的回覆,我已經按要求添加了代碼,我感覺它的冬眠做了不正確的設置,可能是由於我做了一些奇怪的事情。 – Arthur

+0

@Arthur你是對的,沒有想象可以在這裏完成。嘗試清理你的環境。關閉網絡服務器,刪除部署,清理和重建項目,並最終嘗試全新部署。 – Antoniossss

+0

清潔完所有可清潔物品後出現同樣的錯誤。神祕仍在繼續。 – Arthur

0

我用你的確切類Player執行這段代碼並沒有得到錯誤在所有的人創造的球員。一行正確插入表USERS中。

public class TestPlayer { 

public static void main(String[] args) { 

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("TestDB"); 

    EntityManager em = emf.createEntityManager(); 

    Session session = (Session) em.getDelegate(); 

    Transaction tx = session.beginTransaction(); 

    try { 

     Player p = new Player(); 
     p.setEmail("email"); 

     session.save(p); 

     tx.commit(); 

    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 
     em.close(); 
     emf.close(); 
    } 

} 
} 

問題不在實體類中。也許您所做的某些更改無法在部署時正確刷新。