Question

I'm getting stuck in configuring JPA for Mongodb.

Here is my stack exception :

[EL Config]: connection: 2014-03-31 10:48:24.171--ServerSession(1883377923)--Connection(1001688235)--Thread(Thread[main,5,smarttrade])--disconnect
[EL Severe]: ejb: 2014-03-31 10:48:24.181--ServerSession(1883377923)--Thread(Thread[main,5,smarttrade])--java.lang.ClassCastException: org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform cannot be cast to org.eclipse.persistence.internal.databaseaccess.DatabasePlatform
    at org.eclipse.persistence.sequencing.TableSequence.onConnect(TableSequence.java:168)
    at org.eclipse.persistence.sequencing.Sequence.onConnect(Sequence.java:270)
    at org.eclipse.persistence.internal.sequencing.SequencingManager.onConnectSequences(SequencingManager.java:927)
    at org.eclipse.persistence.internal.sequencing.SequencingManager.onConnectInternal(SequencingManager.java:747)
    at org.eclipse.persistence.internal.sequencing.SequencingManager.onConnect(SequencingManager.java:700)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeSequencing(DatabaseSessionImpl.java:282)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:636)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:632)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:568)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:799)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.login(DatabaseSessionImpl.java:756)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:241)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:685)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:304)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:302)
    at com.smarttrade.tick.engine.TickEngine.start(TickEngine.java:287)

My persistence.xml is defined as follow :

<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
   version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
   <persistence-unit name="mongo" transaction-type="RESOURCE_LOCAL">
      <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
      <class>com.smarttrade.tick.jpa.Instrument</class>
      <class>com.smarttrade.tick.jpa.Snapshot</class>
      <exclude-unlisted-classes>false</exclude-unlisted-classes>
      <properties>
         <property name="eclipselink.target-database"
            value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform" />
         <property name="eclipselink.nosql.connection-spec"
            value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec" />
         <property name="eclipselink.nosql.property.mongo.port" value="27017" />
         <property name="eclipselink.nosql.property.mongo.host" value="localhost" />
         <property name="eclipselink.nosql.property.mongo.db" value="mydb" />
         <property name="eclipselink.logging.level" value="FINEST" />
      </properties>
   </persistence-unit>
</persistence>

I have two entity classes :

@Entity
@NoSql(dataFormat = DataFormatType.MAPPED)
public class Instrument {

    @Id
    String securityID = null;

    @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH })
    Set<Snapshot> snapshots = new HashSet<Snapshot>();

    // constructor, getters and setters 
    ...
}

and

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@NoSql(dataFormat = DataFormatType.MAPPED)
public class Snapshot {

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    double id;

    @Temporal(TemporalType.TIMESTAMP)
    Date snapshotTs;

    String bestOfferOwn;
    .. // other fields

    // constructor, getters and setters 
    ...
}

And here it's how I call it :

EntityManagerFactory factory = Persistence.createEntityManagerFactory("mongo");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Instrument i = new Instrument("JAP_USDEUR");
Snapshot s = new Snapshot(new Date(), "LP_one", 20000, 3.1, new Date(), "LP_two", 10000, 3.0, new Date());
Snapshot s2 = new Snapshot(new Date(), "LP_rg", 500, 4.9, new Date(), "LP_zet", 6000, 5.1, new Date());
i.getSnapshots().add(s);
i.getSnapshots().add(s2);
em.persist(i);

em.getTransaction().commit();
em.close();

The factory is created, and then it seems that that EntityManager make the connection, but after I have this cast exception. I found a post about it (Eclipselink with MongoDB java.lang.ClassCastException)but I had already added the classes in my persistence.xml.

Like I'm really a newbie in jpa I might have set something wrong, but I can't find out what.

Was it helpful?

Solution

I finally find out where was my problem. I used a GenerationType.TABLE for my Snaphot's Id generation strategy, but this is not supported in nosql. I just changed :

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
double id;

to

@Id
@GeneratedValue
double id;

and it worked

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top