gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:39:56 25 4
gpt4 key购买 nike

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

最佳答案

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

关于mysql - 如何使用 mysql 使用 JDO 将类持久保存到数据库,出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12714045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com