如何使用mysql使用JDO将类持久化到数据库,出现错误

时间:2012-10-03 17:50:33

标签: mysql persistence jdo

我正在尝试使用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进行映射?我在运行时增强类,而不是在构建时。

1 个答案:

答案 0 :(得分:1)

不要看你在那里尝试的是怎样的。你增强了一些类,但这会返回增强类的字节;它不会神奇地更新已加载到CLASSPATH中的已编译类。要做到这一点,您必须使用自定义类加载器,按http://www.datanucleus.org/servlet/wiki/pages/viewpage.action?pageId=6619188