Welcome Guest  |  Register  |  Login
Login Name Password
  Search  
  Index  | Recent Threads  | Unanswered Threads  | Who's Online  | User List  | Help


Quick Go »

No member browsing this thread
Thread Status: Active
Total posts in this thread: 2
[ Jump to Last Post ]
Post new Thread
Author
Previous Thread This topic has been viewed 77 times and has 1 reply Next Thread
Male bebone
Newbie



France
Joined: Feb 4, 2010
Post Count: 3
Status: Offline
Reply to this Post  Reply with Quote 
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]
[Feb 4, 2010 12:04:22 PM] Show Printable Version of Post    View Member Profile    Send Private Message    Hidden to Guest [Link] Report threatening or abusive post: please login first  Go to top 
Male ganzuoni
Expert
Member's Avatar


Joined: Nov 16, 2005
Post Count: 804
Status: Offline
Reply to this Post  Reply with Quote 
Re: Can't retrieve the 1-to-1 object (null)

You have to read a little about accessing members of PC objects outside a transaction, attach/detach and so on.
I would not suggest your architecture for persistence layer built on top of JDO, anyway (i.e. persistEntity signature for example).

Guido
----------------------------------------
Guido Anzuoni
http://www.objectmagic.org
[Feb 4, 2010 4:24:45 PM] Show Printable Version of Post    View Member Profile    Send Private Message [Link] Report threatening or abusive post: please login first  Go to top 
[ Jump to Last Post ]
Show Printable Version of Thread  Post new Thread