2014-01-16 43 views
1

根據EclipseLink NoSQL文檔,可以使用XML文件來存儲實體。 [1] 並根據這一信息[2]的強制性屬性和它們的值是:EclipseLink如何處理XML文件?

"eclipselink.target-database"-->"org.eclipse.persistence.eis.adapters.xmlfile.XMLFilePlatform" 

"eclipselink.connection-spec"-->"org.eclipse.persistence.eis.adapters.xmlfile.XMLFileEISConnectionSpec" 

"eclipselink.nosql.property.directory"-->/PATH/TO/XML/DIRECTORY 

我從的EclipseLink MongoDB的例子中使用的代碼[3](其中工作的很好的MongoDB),並試圖編輯它以某種方式持續存在於XML文件中。

的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> 
    <persistence-unit name="asd" transaction-type="RESOURCE_LOCAL"> 
    <class>model.Order</class> 
    <class>model.OrderLine</class> 
    <class>model.Address</class> 
    <class>model.Customer</class> 
    <properties> 
     <property name="eclipselink.target-database" value="org.eclipse.persistence.eis.adapters.xmlfile.XMLFilePlatform"/> 
     <property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.eis.adapters.xmlfile.XMLFileEISConnectionSpec"/> 
     <property name="eclipselink.nosql.property.directory" value="/Users/dp/xmlfiles"/> 
     <property name="eclipselink.logging.level" value="FINEST"/> 
    </properties> 
    </persistence-unit> 
</persistence> 

實體:

@Embeddable 
@NoSql 
public class Address implements Serializable { 
    private String street; 
    private String city; 
    private String province; 
    private String country; 
    private String postalCode; 

@Entity 
@NoSql(dataType="customer") 
public class Customer implements Serializable { 
    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE) 
    @Field(name="_id") 
    private String id; 
    @Basic 
    private String name; 

@Entity 
@NoSql(dataType="order") 
public class Order implements Serializable { 
    /* The id uses the generated OID (UUID) from Mongo. */ 
    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE) 
    @Field(name="_id") 
    private long id; 
    @Basic 
    private String description; 
    @Embedded 
    private Address deliveryAddress; 
    @ElementCollection 
    private List<OrderLine> orderLines; 
    @ManyToOne(fetch=FetchType.LAZY) 
    private Customer customer; 

@Embeddable 
@NoSql 
public class OrderLine implements Serializable { 
    @Basic 
    private int lineNumber; 
    @Basic 
    private String description; 
    @Basic 
    private double cost = 0; 

當我試圖得到一個EntityManager

EntityManager em = factory.createEntityManager(); 

會有拋出ClastCastException說:

org.eclipse.persistence.eis.adapters.xmlfile.XMLFilePlatform不能轉換到org.eclipse.persistence.internal.databaseaccess.DatabasePlatform

我不理解,因爲這個屬性/值匹配取自EclipseLink文檔。那麼,我錯過了如何將XML文件用於持久化實體與EclipseLink?

所有日誌:

[EL Finest]: jpa: 2014-01-16 15:31:47.764--ServerSession(962253337)--Thread(Thread[main,5,main])--Begin predeploying Persistence Unit asd; session file:/Users/dp/NetBeansProjects/XMLDSexampleNOTWEB /target/classes/_asd; state Initial; factoryCount 0 
[EL Finest]: properties: 2014-01-16 15:31:47.781--ServerSession(962253337)--Thread(Thread[main,5,main])--property=eclipselink.orm.throw.exceptions; default value=true 
[EL Finest]: properties: 2014-01-16 15:31:47.781--ServerSession(962253337)--Thread(Thread[main,5,main])--property=eclipselink.multitenant.tenants-share-emf; default value=true 
[EL Finest]: properties: 2014-01-16 15:31:47.781--ServerSession(962253337)--Thread(Thread[main,5,main])--property=eclipselink.multitenant.tenants-share-cache; default value=false 
[EL Finer]: metadata: 2014-01-16 15:31:47.799--ServerSession(962253337)--Thread(Thread[main,5,main])--Searching for default mapping file in file:/Users/dp/NetBeansProjects/XMLDSexampleNOTWEB%20/target/classes/ (There is no English translation for this message.) 
[EL Finer]: metadata: 2014-01-16 15:31:47.803--ServerSession(962253337)--Thread(Thread[main,5,main])--Searching for default mapping file in file:/Users/dp/NetBeansProjects/XMLDSexampleNOTWEB%20/target/classes/ (There is no English translation for this message.) 
[EL Config]: metadata: 2014-01-16 15:31:47.934--ServerSession(962253337)--Thread(Thread[main,5,main])--The access type for the persistent class [class model.Order] is set to [FIELD]. 
[EL Config]: metadata: 2014-01-16 15:31:47.969--ServerSession(962253337)--Thread(Thread[main,5,main])--The target class (reference) class for the element collection mapping element [field orderLines] is being defaulted to: class model.OrderLine. 
[EL Config]: metadata: 2014-01-16 15:31:47.977--ServerSession(962253337)--Thread(Thread[main,5,main])--The target entity (reference) class for the many to one mapping element [field customer] is being defaulted to: class model.Customer. 
[EL Config]: metadata: 2014-01-16 15:31:47.977--ServerSession(962253337)--Thread(Thread[main,5,main])--The access type for the persistent class [class model.Customer] is set to [FIELD]. 
[EL Config]: metadata: 2014-01-16 15:31:47.978--ServerSession(962253337)--Thread(Thread[main,5,main])--The access type for the persistent class [class model.OrderLine] is set to [FIELD]. 
[EL Config]: metadata: 2014-01-16 15:31:47.979--ServerSession(962253337)--Thread(Thread[main,5,main])--The access type for the persistent class [class model.Address] is set to [FIELD]. 
[EL Config]: metadata: 2014-01-16 15:31:47.98--ServerSession(962253337)--Thread(Thread[main,5,main])--The alias name for the entity class [class model.Order] is being defaulted to: Order. 
[EL Config]: metadata: 2014-01-16 15:31:48.016--ServerSession(962253337)--Thread(Thread[main,5,main])--The column name for element [description] is being defaulted to: DESCRIPTION. 
[EL Config]: metadata: 2014-01-16 15:31:48.016--ServerSession(962253337)--Thread(Thread[main,5,main])--The column name for element [description] is being defaulted to: DESCRIPTION. 
[EL Config]: metadata: 2014-01-16 15:31:48.017--ServerSession(962253337)--Thread(Thread[main,5,main])--The column name for element [lineNumber] is being defaulted to: LINENUMBER. 
[EL Config]: metadata: 2014-01-16 15:31:48.017--ServerSession(962253337)--Thread(Thread[main,5,main])--The column name for element [cost] is being defaulted to: COST. 
[EL Config]: metadata: 2014-01-16 15:31:48.018--ServerSession(962253337)--Thread(Thread[main,5,main])--The column name for element [postalCode] is being defaulted to: POSTALCODE. 
[EL Config]: metadata: 2014-01-16 15:31:48.018--ServerSession(962253337)--Thread(Thread[main,5,main])--The column name for element [street] is being defaulted to: STREET. 
[EL Config]: metadata: 2014-01-16 15:31:48.018--ServerSession(962253337)--Thread(Thread[main,5,main])--The column name for element [province] is being defaulted to: PROVINCE. 
[EL Config]: metadata: 2014-01-16 15:31:48.019--ServerSession(962253337)--Thread(Thread[main,5,main])--The column name for element [city] is being defaulted to: CITY. 
[EL Config]: metadata: 2014-01-16 15:31:48.019--ServerSession(962253337)--Thread(Thread[main,5,main])--The column name for element [country] is being defaulted to: COUNTRY. 
[EL Config]: metadata: 2014-01-16 15:31:48.02--ServerSession(962253337)--Thread(Thread[main,5,main])--The alias name for the entity class [class model.Customer] is being defaulted to: Customer. 
[EL Config]: metadata: 2014-01-16 15:31:48.02--ServerSession(962253337)--Thread(Thread[main,5,main])--The column name for element [name] is being defaulted to: NAME. 
[EL Config]: metadata: 2014-01-16 15:31:48.03--ServerSession(962253337)--Thread(Thread[main,5,main])--The primary key column name for the mapping element [field customer] is being defaulted to: _id. 
[EL Config]: metadata: 2014-01-16 15:31:48.03--ServerSession(962253337)--Thread(Thread[main,5,main])--The foreign key column name for the mapping element [customer] is being defaulted to: CUSTOMER__id. 
[EL Config]: metadata: 2014-01-16 15:31:48.041--ServerSession(962253337)--Thread(Thread[main,5,main])--The column name for element [orderLines] is being defaulted to: ORDERLINES. 
[EL Config]: metadata: 2014-01-16 15:31:48.041--ServerSession(962253337)--Thread(Thread[main,5,main])--The column name for element [deliveryAddress] is being defaulted to: DELIVERYADDRESS. 
[EL Finest]: jpa: 2014-01-16 15:31:48.042--ServerSession(962253337)--Thread(Thread[main,5,main])--End predeploying Persistence Unit asd; session file:/Users/dp/NetBeansProjects/XMLDSexampleNOTWEB /target/classes/_asd; state Predeployed; factoryCount 0 
[EL Finer]: weaver: 2014-01-16 15:31:48.042--Thread(Thread[main,5,main])--JavaSECMPInitializer - transformer is null. 
[EL Finest]: jpa: 2014-01-16 15:31:48.042--ServerSession(962253337)--Thread(Thread[main,5,main])--Begin predeploying Persistence Unit asd; session file:/Users/dp/NetBeansProjects/XMLDSexampleNOTWEB /target/classes/_asd; state Predeployed; factoryCount 0 
[EL Finest]: jpa: 2014-01-16 15:31:48.042--ServerSession(962253337)--Thread(Thread[main,5,main])--End predeploying Persistence Unit asd; session file:/Users/dp/NetBeansProjects/XMLDSexampleNOTWEB /target/classes/_asd; state Predeployed; factoryCount 1 

Testing persist() of orders and customers. 

[EL Finest]: jpa: 2014-01-16 15:31:48.046--ServerSession(962253337)--Thread(Thread[main,5,main])--Begin deploying Persistence Unit asd; session file:/Users/dp/NetBeansProjects/XMLDSexampleNOTWEB /target/classes/_asd; state Predeployed; factoryCount 1 
[EL Finer]: 2014-01-16 15:31:48.053--ServerSession(962253337)--Thread(Thread[main,5,main])--Could not initialize Validation Factory. Encountered following exception: java.lang.NoClassDefFoundError: javax/validation/Validation 
[EL Finest]: properties: 2014-01-16 15:31:48.056--ServerSession(962253337)--Thread(Thread[main,5,main])--property=eclipselink.logging.level; value=FINEST; translated value=FINEST 
[EL Finest]: properties: 2014-01-16 15:31:48.056--ServerSession(962253337)--Thread(Thread[main,5,main])--property=eclipselink.logging.level; value=FINEST; translated value=FINEST 
[EL Finest]: properties: 2014-01-16 15:31:48.056--ServerSession(962253337)--Thread(Thread[main,5,main])--property=eclipselink.target-database; value=org.eclipse.persistence.eis.adapters.xmlfile.XMLFilePlatform 
[EL Finest]: properties: 2014-01-16 15:31:48.14--ServerSession(962253337)--Thread(Thread[main,5,main])--property=eclipselink.nosql.connection-spec; value=org.eclipse.persistence.eis.adapters.xmlfile.XMLFileEISConnectionSpec 
[EL Info]: 2014-01-16 15:31:48.142--ServerSession(962253337)--Thread(Thread[main,5,main])--EclipseLink, version: Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5 
[EL Config]: connection: 2014-01-16 15:31:48.144--ServerSession(962253337)--Connection(1165994758)--Thread(Thread[main,5,main])--connecting(EISLogin(
    platform=> XMLFilePlatform 
    user name=> "" 
    XMLFileEISConnectionSpec() 
)) 
[EL Config]: connection: 2014-01-16 15:31:48.149--ServerSession(962253337)--Connection(1087133350)--Thread(Thread[main,5,main])--Connected: 
    User: 
    Database: EclipseLink XML File JCA Adapter Version: 2.5.1 
[EL Finest]: connection: 2014-01-16 15:31:48.149--ServerSession(962253337)--Connection(1087133350)--Thread(Thread[main,5,main])--Connection acquired from connection pool [default]. 
[EL Finest]: connection: 2014-01-16 15:31:48.149--ServerSession(962253337)--Connection(1087133350)--Thread(Thread[main,5,main])--Connection released to connection pool [default]. 
[EL Config]: connection: 2014-01-16 15:31:48.153--ServerSession(962253337)--Connection(1087133350)--Thread(Thread[main,5,main])--disconnect 
[EL Finer]: cache: 2014-01-16 15:31:48.153--ServerSession(962253337)--Thread(Thread[main,5,main])--initialize identitymaps 
[EL Info]: connection: 2014-01-16 15:31:48.154--ServerSession(962253337)--Thread(Thread[main,5,main])--file:/Users/dp/NetBeansProjects/XMLDSexampleNOTWEB /target/classes/_asd logout successful 
[EL Config]: connection: 2014-01-16 15:31:48.154--ServerSession(962253337)--Connection(1165994758)--Thread(Thread[main,5,main])--disconnect 
[EL Severe]: ejb: 2014-01-16 15:31:48.157--ServerSession(962253337)--Thread(Thread[main,5,main])--java.lang.ClassCastException: org.eclipse.persistence.eis.adapters.xmlfile.XMLFilePlatform 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.sequencing.NativeSequence.onConnect(NativeSequence.java:185) 
    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 example.Test.testPersist(Test.java:62) 
    at example.Test.main(Test.java:52) 

[EL Finest]: jpa: 2014-01-16 15:31:48.158--ServerSession(962253337)--Thread(Thread[main,5,main])--End deploying Persistence Unit asd; session file:/Users/dp/NetBeansProjects/XMLDSexampleNOTWEB /target/classes/_asd; state DeployFailed; factoryCount 1 
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.EntityManagerSetupException 
Exception Description: Deployment of PersistenceUnit [asd] failed. Close all factories for this PersistenceUnit. 
Internal Exception: java.lang.ClassCastException: org.eclipse.persistence.eis.adapters.xmlfile.XMLFilePlatform cannot be cast to org.eclipse.persistence.internal.databaseaccess.DatabasePlatform 
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createDeployFailedPersistenceException(EntityManagerSetupImpl.java:820) 
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:760) 
    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 example.Test.testPersist(Test.java:62) 
    at example.Test.main(Test.java:52) 
Caused by: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.EntityManagerSetupException 
Exception Description: Deployment of PersistenceUnit [asd] failed. Close all factories for this PersistenceUnit. 
Internal Exception: java.lang.ClassCastException: org.eclipse.persistence.eis.adapters.xmlfile.XMLFilePlatform cannot be cast to org.eclipse.persistence.internal.databaseaccess.DatabasePlatform 
    at org.eclipse.persistence.exceptions.EntityManagerSetupException.deployFailed(EntityManagerSetupException.java:238) 
    ... 8 more 
Caused by: java.lang.ClassCastException: org.eclipse.persistence.eis.adapters.xmlfile.XMLFilePlatform 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.sequencing.NativeSequence.onConnect(NativeSequence.java:185) 
    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) 
    ... 6 more 

[1] http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/NoSQL/Persistence_Units

[2] http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/NoSQL/About

[3] http://wiki.eclipse.org/EclipseLink/Examples/JPA/NoSQL

回答

0

我得到了相同的異常實施mongodb的持久性: Eclipslink with MongoDB - cast exception

我的問題是關於我使用的價值生成策略,它不被mongo支持。

如果你讓它有:@GeneratedValue(沒有策略規範),它將使用默認策略,這可能會解決你的問題。

希望它會有所幫助。