lundi 6 juin 2016

Hibernate DAO setting object value as object

I am trying to use Hibernate on my project << all sources if wanted, I try to create and save an object player on startup, I get the following error:

===============================================================================
== WELCOME TO RESTX SHELL - 0.34 - type `help` for help on available commands
===============================================================================
centaurus> app run
compiling App... [DONE]
copying resources... [DONE]
starting centaurus.AppServer... - type `stop` to stop it and go back to restx shell
2016-06-06 12:35:57,482 [main            ] [          ] INFO  org.eclipse.jetty.server.Server - jetty-8.1.8.v20121106
LoginService=HashLoginService[null] identityService=org.eclipse.jetty.security.DefaultIdentityService@5872e796
2016-06-06 12:35:57,907 [main            ] [          ] INFO  restx.RestxMainRouterFactory - LOADING MAIN ROUTER
2016-06-06 12:35:57,932 [main            ] [          ] INFO  restx.RestxMainRouterFactory - 
--------------------------------------
 -- RESTX >> LOAD ON REQUEST << >> DEV MODE << >> AUTO COMPILE <<
 -- for admin console,
 --   VISIT http://ift.tt/1TIpafU
 --

2016-06-06 12:35:57,959 [main            ] [          ] INFO  restx.Apps - can't enable Apidocs doclet: make sure tools.jar is in your classpath
2016-06-06 12:35:58,773 [pool-1-thread-1 ] [          ] INFO  restx.classloader.CompilationManager - compilation finished: 2 sources compiled in 691.2 ms
2016-06-06 12:35:58,922 [main            ] [          ] INFO  restx.Apps - can't enable Apidocs doclet: make sure tools.jar is in your classpath
2016-06-06 12:35:58,951 [main            ] [          ] INFO  restx.classloader.CompilationManager - watching for changes in [src/main/java, src/main/resources]; current location is /home/arthur/projects/elorhia-api/.
2016-06-06 12:35:59,046 [main            ] [          ] INFO  org.hibernate.Version - HHH000412: Hibernate Core {5.1.0.Final}
2016-06-06 12:35:59,047 [main            ] [          ] INFO  org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
2016-06-06 12:35:59,050 [main            ] [          ] INFO  org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist
2016-06-06 12:35:59,421 [main            ] [          ] INFO  o.h.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2016-06-06 12:35:59,504 [main            ] [          ] WARN  org.hibernate.orm.connections - HHH10001002: Using Hibernate built-in connection pool (not for production use!)
2016-06-06 12:35:59,514 [main            ] [          ] INFO  org.hibernate.orm.connections - HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/andromeda]
2016-06-06 12:35:59,515 [main            ] [          ] INFO  org.hibernate.orm.connections - HHH10001001: Connection properties: {user=api, password=****}
2016-06-06 12:35:59,515 [main            ] [          ] INFO  org.hibernate.orm.connections - HHH10001003: Autocommit mode: false
2016-06-06 12:35:59,517 [main            ] [          ] INFO  o.h.e.j.c.i.DriverManagerConnectionProviderImpl - HHH000115: Hibernate connection pool size: 1 (min=1)
2016-06-06 12:35:59,698 [main            ] [          ] INFO  org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2016-06-06 12:36:00,075 [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 org.joda.time.DateTime centaurus.domain.Player.created] by reflection for persistent property [centaurus.domain.Player#created] : centaurus.domain.Player@7f95327
    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.player.PlayerDAOimpl.savePlayer(PlayerDAOimpl.java:35)
    at centaurus.Dbmaintain.start(Dbmaintain.java:25)
    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 org.joda.time.DateTime field centaurus.domain.Player.created to centaurus.domain.Player
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
    at java.lang.reflect.Field.get(Field.java:379)
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:39)
    ... 40 more

I have stepped through my program and it seems to have a valid object passed the the hibernate save function, and somewhere inside it throws an error. I have tried removing the created field, at which point it then complains about a string field with the same error, trying to set it as a Player object itself.

here is my DAOimpl.class

package centaurus.service.player;

import centaurus.domain.Player;

import centaurus.service.HibernateUtils;
import centaurus.service.player.PlayerDAO;
import restx.factory.Component;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import javax.inject.Named;
import javax.inject.Singleton;

@Component
public class PlayerDAOimpl implements PlayerDAO {
    private static HibernateUtils hibernateUtils;

    public PlayerDAOimpl(@Named("HibernateUtils") HibernateUtils hibernateUtils) {
        this.hibernateUtils = hibernateUtils;
    }

    public Player savePlayer(Player player){
        Session session = hibernateUtils.getFactory().openSession();
        Transaction tx = null;
        Integer playerID = null;
        try{
            tx = session.beginTransaction();
            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 = hibernateUtils.getFactory().openSession();
        try{
            Player player = (Player)session.get(Player.class, playerId);
            return player;
        }catch (HibernateException e) {

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

I have googled and googled and tried as many hibernate tutorials as i can find and I still have this issue. I don't understand why hibernate is trying to set a field as an object, I have my annotations.

incase its wanted here is my domain object player:

package centaurus.domain;

import org.joda.time.DateTime;
import javax.persistence.*;

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

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

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

    @Column(name="CREATED")
    private DateTime created;

    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;
    }

    public DateTime getCreated() {
        return created;
    }

    public void setCreated(DateTime created) {
        this.created = created;
    }

}

and here is the calling class:

package centaurus;

import centaurus.domain.Player;
import centaurus.service.player.PlayerDAO;
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) {

        }
    }
}

Please does anyone know how to solve this issue, thanks.





Aucun commentaire:

Enregistrer un commentaire