bebone
Newbie
France
Joined: Feb 4, 2010
Post Count: 3
Status:
Offline
|
|
|
Can't retrieve the 1-to-1 object (null)
|
Hi,
I try to use Datanucleus to store object with 1-to-1 relation (later i will use 1-to-many relation) I use the "datanucleus-samples-jdo-tutorial-2.0" project and use the class from the given tutorial http://www.datanucleus.org/products/accessplatform_1_1/jdo/orm/one_to_one.html
As you can see, an account has a 1-to-1 relation to an user. I create the following classes:
package org.datanucleus.samples.jdo.tutorial;
import javax.jdo.annotations.PersistenceCapable;
@PersistenceCapable public class Account {
private Long id; private String firstName; private String secondName; private User user;
public Account() { }
public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getSecondName() { return secondName; } public void setSecondName(String secondName) { this.secondName = secondName; } public User getUser() { return user; } public void setUser(User user) { this.user = user; }
}
package org.datanucleus.samples.jdo.tutorial;
import javax.jdo.annotations.PersistenceCapable;
@PersistenceCapable public class User {
private Long id; private String login;
public User() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; }
}
I edit package-hsql.orm with the following code(similar to tutorial website)
<?xml version="1.0"?> <!DOCTYPE orm SYSTEM "file:/javax/jdo/orm.dtd"> <orm> <package name="org.datanucleus.samples.jdo.tutorial">
<class name="User" identity-type="datastore" table="JDO_USER"> <field name="id" primary-key="true"> <column name="USER_ID" /> </field> <field name="login"> <column name="LOGIN" length="20" jdbc-type="VARCHAR" /> </field> </class> <class name="Account" identity-type="datastore" table="JDO_ACCOUNT"> <field name="id" primary-key="true"> <column name="ACCOUNT_ID" /> </field> <field name="firstName"> <column name="FIRSTNAME" length="50" jdbc-type="VARCHAR" /> </field> <field name="secondName"> <column name="SECONDNAME" length="50" jdbc-type="VARCHAR" /> </field> <field name="user" persistence-modifier="persistent"> <column name="USER_ID" /> </field> </class> </package> </orm>
And change the code of the Main.java to do my stuff
...
public class Main { public static void main(String args[]) { // Create a PersistenceManagerFactory for this datastore PersistenceManagerFactory pmf = JDOHelper .getPersistenceManagerFactory("datanucleus.properties");
System.out.println("DataNucleus AccessPlatform with JDO"); System.out.println("===================================");
User user = new User(); user.setId(new Long(1)); user.setLogin("beb"); Account account = new Account(); account.setId(new Long(1)); account.setFirstName("John"); account.setSecondName("Doe"); account.setUser(user); persistEntity(pmf, account); getAllEntity(pmf, Account.class);
...
System.out.println("End of Tutorial"); }
private static void persistEntity(PersistenceManagerFactory pmf, Object object) { // Persistence of a Product and a Book. PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); System.out.println("Persisting products"); pm.makePersistent(object); tx.commit(); System.out.println("Entity have been persisted"); } finally { if (tx.isActive()) { tx.rollback(); } pm.close(); } System.out.println(""); }
private static void getAllEntity(PersistenceManagerFactory pmf, Class<?> clazz) { PersistenceManager pm; Transaction tx; // Basic Extent of all Products pm = pmf.getPersistenceManager(); tx = pm.currentTransaction(); try { tx.begin(); System.out.println("Retrieving Extent for '" + clazz.getSimpleName() + "'"); Extent<?> e = pm.getExtent(clazz, true); Iterator<?> iter = e.iterator(); while (iter.hasNext()) { Object obj = iter.next(); System.out.println("> " + obj); } tx.commit(); } catch (Exception e) { System.out.println("Exception thrown during retrieval of Extent : " + e.getMessage()); } finally { if (tx.isActive()) { tx.rollback(); } pm.close(); } System.out.println(""); }
...
}
When i retrieve the object in the getAllEntity() method, All the attribute of the persisted 'Account' object are well retrieved except the encapsulated 'User' object . I got a Null pointer...
NB: I use maven plugin defined in the tutorial project to enhance classes and launch the project
Thanks in advance
----------------------------------------
[Edit 2 times,
last edit by bebone at Feb 4, 2010 12:50:42 PM]
|