1

我正在尝试使用 JDO 3.1 API 从持久类生成表。

这是我的代码:

class product

@PersistenceCapable
public class Product{

    @PrimaryKey
    @Persistent(valueStrategy=IdGeneratorStrategy.INCREMENT)
    public  long id;

    public String name = null;
    public String description = null;
    public double price = 0.0;

    public Product(){}

    public Product(String name, String desc, double price)
    {
        this.name = name;
        this.description = desc;
        this.price = price;
    }

这是我的主要内容:

public static void main(String[] args) {

        JDOEnhancer enhancer = JDOHelper.getEnhancer();
            enhancer.setVerbose(true);
            enhancer.addClasses(Product.class.getName());
        // enhancer.addPersistenceUnit("OnlineStore");
            // add entities to be enhanced to this list...
            enhancer.enhance();


        Properties properties = new Properties();
        properties.setProperty("javax.jdo.PersistenceManagerFactoryClass",
                        "org.datanucleus.api.jdo.JDOPersistenceManagerFactory");



        properties.setProperty("datanucleus.ConnectionDriverName","com.mysql.jdbc.Driver");
        properties.setProperty("datanucleus.ConnectionURL","jdbc:mysql://localhost/example");
        properties.setProperty("datanucleus.ConnectionUserName","root");
        properties.setProperty("datanucleus.ConnectionPassword","root");    

        PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);
        PersistenceManager pm = pmf.getPersistenceManager();


        Transaction tx=pm.currentTransaction();
        try
        {
            tx.begin();
            Product product = new Product("Sony Discman", "A standard discman from Sony", 49.99);
            pm.makePersistent(new Product());
            tx.commit();
        }
        finally
        {
            if (tx.isActive())
            {
                tx.rollback();
            }
            pm.close();
        }

    }

我有一个错误:

No hay ningun generador de valor para la estrategia "custom" para este base de datos. Por favor, lee en la documentaci�n sobre las estrategias que esta apoyado para este base de datos.
org.datanucleus.exceptions.NucleusUserException: No hay ningun generador de valor para la estrategia "custom" para este base de datos. Por favor, lee en la documentaci�n sobre las estrategias que esta apoyado para este base de datos.
    at org.datanucleus.store.AbstractStoreManager.getStrategyValue(AbstractStoreManager.java:1564)
    at org.datanucleus.state.JDOStateManager.populateStrategyFields(JDOStateManager.java:690)
    at org.datanucleus.state.JDOStateManager.initialiseForPersistentNew(JDOStateManager.java:362)
    at org.datanucleus.state.StateManagerFactory.newForPersistentNew(StateManagerFactory.java:187)
    at org.datanucleus.state.ObjectProviderFactory.newForPersistentNew(ObjectProviderFactory.java:145)
    at org.datanucleus.ObjectManagerImpl.persistObjectInternal(ObjectManagerImpl.java:1895)
    at org.datanucleus.ObjectManagerImpl.persistObjectWork(ObjectManagerImpl.java:1745)
    at org.datanucleus.ObjectManagerImpl.persistObject(ObjectManagerImpl.java:1593)
    at org.datanucleus.api.jdo.JDOPersistenceManager.jdoMakePersistent(JDOPersistenceManager.java:731)
    at org.datanucleus.api.jdo.JDOPersistenceManager.makePersistent(JDOPersistenceManager.java:756)
    at com.test.Main.main(Main.java:47)

有没有人使用 JDO 类和 MySQL 进行映射?我在运行时增强类,而不是在构建时。

4

1 回答 1

1

看不到你在那里尝试的东西是如何工作的。您增强了某些类,但这将返回增强类的字节;它不会神奇地更新已经加载到 CLASSPATH 中的编译类。为此,您必须使用自定义类加载器,根据http://www.datanucleus.org/servlet/wiki/pages/viewpage.action?pageId=6619188

于 2012-10-04T08:06:32.913 回答