带有MongoDB的Eclipselink-强制转换例外 [英] Eclipselink with MongoDB - cast exception

查看:102
本文介绍了带有MongoDB的Eclipselink-强制转换例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法为Mongodb配置JPA.

I'm getting stuck in configuring JPA for Mongodb.

这是我的堆栈异常:

[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)

我的persistence.xml定义如下:

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>

我有两个实体类:

@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 
    ...
}

@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 
    ...
}

这就是我的称呼:

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();

创建了工厂,然后似乎EntityManager建立了连接,但是在出现此强制转换异常之后.我找到了有关它的帖子(与MongoDB的Eclipselink java.lang.ClassCastException )但是我已经在我的persistence.xml中添加了这些类.

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.

就像我真的是jpa的新手一样,我可能设置了一些错误,但是我找不到答案.

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

推荐答案

我终于找出问题所在.我为Snaphot的ID生成策略使用了GenerationType.TABLE,但是nosql不支持此功能. 我刚刚改变了:

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;

@Id
@GeneratedValue
double id;

它奏效了

这篇关于带有MongoDB的Eclipselink-强制转换例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆