【问题标题】:Simple local JPA2HBase App with DataNucleus带有 DataNucleus 的简单本地 JPA2HBase 应用程序
【发布时间】:2011-09-01 14:40:33
【问题描述】:

我想构建一个简约的本地应用程序,通过 JPA2 读取/写入 HBase,无需 orm.xml 和 maven2。 因此,我将 Eclipse 与为项目启用了 Enhancer 的 DataNucleus 插件一起使用。

灵感来自 http://matthiaswessendorf.wordpress.com/2010/03/17/apache-hadoop-hbase-plays-nice-with-jpa/ 我得到了以下实体:

@Entity
@Table(name="account_table")
public class Account
{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private String id;

    String firstName = null;
    String lastName = null;
    int level = 0;
    @Embedded
    Login login = null;

    public Account() {      }

    public Account(String firstName, String lastName, int level, Login login) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.level = level;
        this.login = login;
    }

@Embeddable
public class Login
{

    private String login = null;
    private String password = null;

    public Login() {
        // TODO Auto-generated constructor stub
    }

    public Login(String login, String password) {
        super();
        this.login = login;
        this.password = password;
    }
 }

src/META-INF/persistence.xml

<persistence
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
    −
    <persistence-unit name="hbase-addressbook"
        transaction-type="RESOURCE_LOCAL">
        <class>de.syrtec.jpa2hbase.entities.Login</class>
        <class>de.syrtec.jpa2hbase.entities.Account</class>

        <properties>
            <property name="datanucleus.ConnectionURL" value="hbase" />
            <property name="datanucleus.ConnectionUserName" value="" />
            <property name="datanucleus.ConnectionPassword" value="" />
            <property name="datanucleus.autoCreateSchema" value="true" />
            <property name="datanucleus.validateTables" value="false" />
            <property name="datanucleus.Optimistic" value="false" />
            <property name="datanucleus.validateConstraints" value="false" />
        </properties>
    </persistence-unit>
</persistence>

DAO:

public class TestDAO {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("hbase-addressbook");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = null;

        Account a1 = new Account("myPre", "mySur", 1, new Login("a", "b"));

        tx = em.getTransaction();
        tx.begin(); 
            em.persist(a1);
        tx.commit();
    }
}

但是当测试DAO的第一行被执行时...

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("hbase-addressbook");

..我明白了:

11/09/01 06:57:05 INFO DataNucleus.MetaData: Class "de.syrtec.jpa2hbase.entities.Account" has been specified with JPA annotations so using those.
11/09/01 06:57:05 INFO DataNucleus.MetaData: Class "de.syrtec.jpa2hbase.entities.Login" has been specified with JPA annotations so using those.
Exception in thread "main" javax.persistence.PersistenceException: Explicit persistence provider error(s) occurred for "hbase-addressbook" after trying the following discovered implementations: org.datanucleus.api.jpa.PersistenceProviderImpl from provider: org.datanucleus.api.jpa.PersistenceProviderImpl
    at javax.persistence.Persistence.createPersistenceException(Persistence.java:242)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:184)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:70)
    at de.syrtec.jpa2hbase.start.TestDAO.main(TestDAO.java:15)
Caused by: org.datanucleus.exceptions.NucleusUserException: Errors were encountered when loading the MetaData for the persistence-unit "hbase-addressbook". See the nested exceptions for details
    at org.datanucleus.metadata.MetaDataManager.loadPersistenceUnit(MetaDataManager.java:879)
    at org.datanucleus.api.jpa.JPAEntityManagerFactory.initialiseNucleusContext(JPAEntityManagerFactory.java:745)
    at org.datanucleus.api.jpa.JPAEntityManagerFactory.<init>(JPAEntityManagerFactory.java:422)
    at org.datanucleus.api.jpa.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:91)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:150)
    ... 2 more
Caused by: org.datanucleus.exceptions.ClassNotResolvedException: Class "−

        de.syrtec.jpa2hbase.entities.Login" was not found in the CLASSPATH. Please check your specification and your CLASSPATH.
    at org.datanucleus.JDOClassLoaderResolver.classForName(JDOClassLoaderResolver.java:247)
    at org.datanucleus.JDOClassLoaderResolver.classForName(JDOClassLoaderResolver.java:412)
    at org.datanucleus.metadata.MetaDataManager.loadPersistenceUnit(MetaDataManager.java:859)
    ... 6 more

在我运行 DAO 之前,我成功触发了 datanucleus 的类增强:

DataNucleus Enhancer (version 3.0.0.release) : Enhancement of classes
DataNucleus Enhancer completed with success for 2 classes. Timings : input=623 ms, enhance=101 ms, total=724 ms. Consult the log for full details

虽然我不明白,尽管激活了项目的自动增强功能,但增强不会自动触发(参考日志)..

有人知道为什么找不到我的实体吗?

【问题讨论】:

    标签: eclipse jpa jpa-2.0 hbase datanucleus


    【解决方案1】:

    persistence.xml 中的减号?

    【讨论】:

    • 这个问题的解决方案太简单了......一位同事给我的建议是删除persistence.xml中的显式类列表......并且tata......它有效:) @DataNucleus:我想知道这个减号是从哪里来的...删除它以防止进一步的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    相关资源
    最近更新 更多