Hi,

I have a problem with my DAO unit test to check my entities.
I have make many tries but I can not find a good solution.
I would like to be able to check the values of each property of
entities but also theirs childs.
I am currently using JPA and spring for the transaction management.
I have set the DAO methods as transactional but not those of the unit
test

I have a JDODetachedFieldAccessException occurs when I tried to access
to the childs element because the
entities have been detached at the end of the transaction.

@Entity
@Table(name = "Account")
public class Account implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key id;

    @Basic
    private String name;

    @OneToMany(mappedBy = "account", cascade = CascadeType.ALL)
    private List<Profile> profiles = new ArrayList<Profile>();

    //
____________________________________________________________________________
    public Account(final String name) {
        this.name = name;
    }
}

@Entity
@Table(name = "Profile")
public class Profile implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key id;

    @ManyToOne(fetch = FetchType.LAZY)
    private Account account;

    //
____________________________________________________________________________
    public Profile() {
    }
}

 //
____________________________________________________________________________
    @Test
    public final void testCreateAccount() throws DaoException {
        Profile profile = new Profile();
        Account account = new Account("NewAccountName1");
        account.getProfiles().add(profile);

        this.accountDao.create(account);

        account = this.accountDao.load(account.getId());
        List<Profile> profiles = account.getProfiles(); <==== ASSERT
    }

I have tried to keep the transaction alive in the unit test methods
but in this case the load function does not work because the query see
a snapshot of the datastore made at the beginning of the transaction.

Is there any good solution to do this?

Thanks for your help

PA

The complete trace of the exception:
javax.jdo.JDODetachedFieldAccessException: You have just attempted to
access field "profiles" yet this field was not detached when you
detached the object. Either dont access this field, or detach it when
detaching the object.
        at
com.website.core.database.model.Account.jdoGetprofiles(Account.java)
        at com.website.core.database.model.Account.getProfiles(Account.java:
23)
        at
unittest.com.website.core.database.dao.TestDAO.testCreateAccount(TestDAO.java:
59)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.junit.runners.model.FrameworkMethod
$1.runReflectiveCall(FrameworkMethod.java:44)
        at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:
15)
        at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:
41)
        at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:
20)
        at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:
28)
        at
org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:
74)
        at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:
31)
        at
org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:
82)
        at
org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:
72)
        at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:
240)
        at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:
50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at
org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:
61)
        at
org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:
70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at
org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:
180)
        at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
49)
        at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:
38)
        at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
467)
        at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
683)
        at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
390)
        at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
197)


-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-java@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to