我有一个类Autor和一个类Dokument,它们处于双向1到n列表关系(1个Autor可以编写几个文档),我想将它们保存到Cloud SQL中。 orm在package.jdo和package-cloudsql.orm中指定。
Dokument类:
public class Autor {
private String aid;
private String vorname;
private String nachname;
private List<Dokument> dokumente;
public Autor() {
}
public Autor(String vorname, String nachname) {
this.vorname = vorname;
this.nachname = nachname;
this.dokumente = new ArrayList<Dokument>();
}
public String getAID() {
return aid;
}
public String getVorname() {
return vorname;
}
public String getNachname() {
return nachname;
}
public List<Dokument> getDokumente() {
return this.dokumente;
}
public void setAID(String aid) {
this.aid = aid;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public void setNachname(String nachname) {
this.nachname = nachname;
}
public void addDokument(Dokument dokument) {
this.dokumente.add(dokument);
}
public void deleteDokument(Dokument dokument) {
this.dokumente.remove(dokument);
}
public void deleteAllDokumente() {
this.dokumente.clear();
}
}
Dokument类:
public class Dokument {
private String did;
private Autor autor;
private String titel;
private String text;
private Date datum;
public Dokument() {
}
public Dokument(Autor autor, String titel, String text, Date datum) {
this.autor = autor;
this.titel = titel;
this.text = text;
this.datum = datum;
}
public Dokument(String titel, String text, Date datum) {
this.autor = null;
this.titel = titel;
this.text = text;
this.datum = datum;
}
public String getDid() {
return did;
}
public Autor getAutor() {
return autor;
}
public String getTitel() {
return titel;
}
public String getText() {
return text;
}
public Date getDatum() {
return datum;
}
public void setDID(String did) {
this.did = did;
}
public void setAutor(Autor autor) {
this.autor = autor;
}
public void setTitel(String titel) {
this.titel = titel;
}
public void setText(String text) {
this.text = text;
}
public void setDatum(Date date) {
this.datum = date;
}
}
package.jdo包含:
<class name="Autor" detachable="true" identity-type="application">
<field name="aid" primary-key="true" persistence-modifier="persistent" value-strategy="identity"/>
<field name="vorname"/>
<field name="nachname" />
<field name="dokumente"/>
</class>
<class name="Dokument" detachable="true" identity-type="application">
<field name="did" primary-key="true" persistence-modifier="persistent" value-strategy="identity"/>
<field name="autor"/>
<field name="titel"/>
<field name="text"/>
<field name="datum"/>
</class>
package-sql.orm包含:
<class name="Autor" detachable="true" persistence-modifier="persistence-capable" table="Autor">
<field name="aid" primary-key="true" persistence-modifier="persistent" value-strategy="identity">
<column name="aid" jdbc-type="bigint" length="20"/>
</field>
<field name="vorname" persistence-modifier="persistent">
<column name="vorname"/>
</field>
<field name="nachname" persistence-modifier="persistent">
<column name="nachname"/>
</field>
<field name="dokumente" persistence-modifier="persistent" mapped-by="autor">
<collection element-type="de.hdm.studienarbeit3.dokumente.Dokument"/>
</field>
</class>
<class name="Dokument" detachable="true" persistence-modifier="persistence-capable" table="Dokument">
<field name="did" primary-key="true" persistence-modifier="persistent" value-strategy="identity">
<column name="did" jdbc-type="bigint" length="20" />
</field>
<field name="autor" persistence-modifier="persistent" default-fetch-group="true">
<column name="autor" jdbc-type="bigint" length="20"/>
<foreign-key name="DOKUMENTAUTOR_FK" delete-action="restrict"/>
</field>
<field name="titel" persistence-modifier="persistent">
<column name="titel"/>
</field>
<field name="text" persistence-modifier="persistent">
<column name="text"/>
</field>
<field name="datum" persistence-modifier="persistent">
<column name="datum"/>
</field>
</class>
所以我想在现有的Autor中添加第二个(第三个等等)Dokument。但每次我坚持一个Dokument,它也创造了一个新的Autor,我选择了Autor的属性。 例如,我有一个名为“Hans Maier”的Autor,并希望与他一起制作另一个Dokument作为Autor,但新的dokument将有一个id为2的Autor,以及名称为“Hans Maier”的东西。我希望新的文件也可以连接到Autor 1。
我的servlet中有这段代码:
if (autoraid=="") {
if (vorname == "" | nachname =="") {
resp.getWriter().println("Bitte Autor auswählen oder Namen vollständig ausfüllen.");
} else {
Autor autor = new Autor(vorname, nachname);
Dokument dokument = new Dokument(autor, titel, text, datum);
autor.addDokument(dokument);
DokumentDAO dokumentDao = new DokumentDAO(pmf);
dokumentDao.addDokument(autor, dokument);
}
} else {
AutorDAO autorDao = new AutorDAO(pmf);
Autor autor = autorDao.getAutor(autoraid);
Dokument dokument = new Dokument(autor, titel, text, datum);
autor.addDokument(dokument);
DokumentDAO dokumentDao = new DokumentDAO(pmf);
dokumentDao.addDokument(autor, dokument);
}
autorDao.getAutor返回它通过键获取的autor(这个工作)和dokumentDao.addDokument()使用pm.makePersistent(dokument)保存给定的dokument
我错过了什么或做错了什么? Datanucleus Documentation说我必须在双向关系http://www.datanucleus.org/products/accessplatform/jdo/orm/relationships.html上使用List时设置两端,我在Dokument创建者和autor.addDokument()上设置Autor,但它不起作用。
如果我在持久化之前读取了dokument.getAutor()。getAID(),则返回我希望它连接到的Autor的正确ID,但在数据库中它是一个新的Autor。我怎样才能使它正常工作?
我已经阅读了这个http://www.onjava.com/pub/a/onjava/excerpt/chap_07/index2.html并且createBook方法正是我想要做的,所以我也测试过了
public void addDokument(Autor autor, Dokument dokument) {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
dokument.setAutor(autor);
autor.addDokument(dokument);
pm.makePersistent(dokument);
tx.commit();
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
在交易中,但同样的结果,创建了一个新的Autor。
任何帮助将不胜感激。
日志说明如下:
Feb 18, 2014 1:23:33 PM org.datanucleus.store.rdbms.mapping.MappedTypeManager addMappedType
Schwerwiegend: User-defined type mapping class "org.datanucleus.store.mapped.mapping.LocalDateMapping" was not found. Please check the mapping file class specifications and your CLASSPATH. The class must be in the CLASSPATH.
Feb 18, 2014 1:23:33 PM org.datanucleus.store.rdbms.mapping.MappedTypeManager addMappedType
Schwerwiegend: User-defined type mapping class "org.datanucleus.store.mapped.mapping.LocalDateTimeMapping" was not found. Please check the mapping file class specifications and your CLASSPATH. The class must be in the CLASSPATH.
Feb 18, 2014 1:23:33 PM org.datanucleus.store.rdbms.mapping.MappedTypeManager addMappedType
Schwerwiegend: User-defined type mapping class "org.datanucleus.store.mapped.mapping.LocalTimeMapping" was not found. Please check the mapping file class specifications and your CLASSPATH. The class must be in the CLASSPATH.
Feb 18, 2014 1:23:39 PM org.datanucleus.store.rdbms.query.ForwardQueryResult closingConnection
Information: Reading in results for query "SELECT FROM de.hdm.studienarbeit3.dokumente.Autor ORDER BY nachname asc" since the connection used is closing
Feb 18, 2014 1:23:52 PM org.datanucleus.store.rdbms.mapping.MappedTypeManager addMappedType
Schwerwiegend: User-defined type mapping class "org.datanucleus.store.mapped.mapping.LocalDateMapping" was not found. Please check the mapping file class specifications and your CLASSPATH. The class must be in the CLASSPATH.
Feb 18, 2014 1:23:52 PM org.datanucleus.store.rdbms.mapping.MappedTypeManager addMappedType
Schwerwiegend: User-defined type mapping class "org.datanucleus.store.mapped.mapping.LocalDateTimeMapping" was not found. Please check the mapping file class specifications and your CLASSPATH. The class must be in the CLASSPATH.
Feb 18, 2014 1:23:52 PM org.datanucleus.store.rdbms.mapping.MappedTypeManager addMappedType
Schwerwiegend: User-defined type mapping class "org.datanucleus.store.mapped.mapping.LocalTimeMapping" was not found. Please check the mapping file class specifications and your CLASSPATH. The class must be in the CLASSPATH.
org.datanucleus.store.mapped.mapping。*曾经在datanucleus-core(至少在版本3.1.3中),但现在在版本3.2.9中。它不在了吗?它去了哪里? datanucleus-rdbms也是版本3.2.9。
System.getProperty(“java.class.path”)
的输出[workspace]\studienarbeit3\war\WEB-INF\classes
[workspace]\studienarbeit3\war\WEB-INF\lib\mysql-connector-java-5.1.28-bin.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\shared\appengine-local-runtime-shared.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\shared\el-api.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\shared\jsp\repackaged-appengine-ant-1.7.1.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\shared\jsp\repackaged-appengine-ant-launcher-1.7.1.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\shared\jsp\repackaged-appengine-jasper-6.0.29.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\shared\jsp\repackaged-appengine-jasper-el-6.0.29.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\shared\jsp\repackaged-appengine-tomcat-juli-6.0.29.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\shared\jsp-api.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\shared\servlet-api.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\appengine-tools-api.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\opt\user\appengine-api-labs\v1\appengine-api-labs.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\opt\user\appengine-endpoints\v1\appengine-endpoints-deps.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\opt\user\appengine-endpoints\v1\appengine-endpoints.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\opt\user\jsr107\v1\appengine-jsr107cache-1.8.9.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\opt\user\jsr107\v1\jsr107cache-1.1.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\user\appengine-api-1.0-sdk-1.8.9.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\opt\user\datanucleus\v3\asm-4.0.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\opt\user\datanucleus\v3\datanucleus-api-jdo-3.2.8.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\opt\user\datanucleus\v3\datanucleus-appengine-3.0.0.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\opt\user\datanucleus\v3\datanucleus-core-3.2.9.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\opt\user\datanucleus\v3\datanucleus-rdbms-3.2.9.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\opt\user\datanucleus\v3\jdo-api-3.1-rc1.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\opt\user\datanucleus\v3\jta-1.1.jar
[workspace]\studienarbeit3\war\WEB-INF\lib\javax.servlet.jsp.jstl-1.2.1.jar
[workspace]\studienarbeit3\war\WEB-INF\lib\javax.servlet.jsp.jstl-api-1.2.1.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\impl\google_sql.jar
[eclipse]\plugins\com.google.appengine.eclipse.sdkbundle_1.8.9\appengine-java-sdk-1.8.9\lib\agent\appengine-agent.jar
答案 0 :(得分:0)
正如Google's compatibility doc所说,他们的GAE /数据存储插件(datanucleus-appengine)仅与DataNucleus 3.1兼容。在DataNucleus Maven回购中有一个build的插件的SVN(与DataNucleus 3.2 / 3.3一起使用)(但是问Google为什么他们懒得发布这个,它已经在他们的SVN中使用了一年或更长时间)。您的其他主题表示您需要DataNucleus 3.2+才能与Google Cloud SQL一起使用。总之,为什么你不能混合和匹配DataNucleus jar版本是非常明显的;选择你正在使用的版本并获得适当的罐子。