Bug 65 : BeanPropertyAssocOne not initialized
Priority 
High
Reported Version 
 
Logged By 
imario
Status 
Fixed
Fixed Version 
1.0.3
Assigned To 
 
Product 
Ebean - core
Duplicate Of 
 
Created 
14/02/2009
Updated 
14/02/2009
Type 
Bug
 
Attachments 
No attachments

For some reasons it seems the BeanPropertyAssocOne.initialize() has not been called and so subsequent access to the targetDescriptor results in an NullPointerException.

I am pretty sure this is not the right patch, but for me it started to work afterwards. Still, as I wrote this might just be a hack.
I am still learning the "in depths" of this framework.

Index: ../ebean/src/com/avaje/ebean/server/deploy/BeanPropertyAssocMany.java
===================================================================
--- ../ebean/src/com/avaje/ebean/server/deploy/BeanPropertyAssocMany.java (revision 127)
+++ ../ebean/src/com/avaje/ebean/server/deploy/BeanPropertyAssocMany.java Sat Feb 14 10:37:23 CET 2009
@@ -310,6 +310,11 @@
if (uids.length == 1 && uids[0].isEmbedded()) {

BeanPropertyAssocOne one = (BeanPropertyAssocOne) uids[0];
+ if (one.getTargetDescriptor() == null)
+ {
+ // I'll consider this as a hack ...
+ one.initialise();
+ }
BeanDescriptor targetDesc = one.getTargetDescriptor();
BeanProperty[] emIds = targetDesc.propertiesBaseScalar();
for (int i = 0; i < emIds.length; i++) {
Index: ../ebean/src/com/avaje/ebean/server/deploy/BeanPropertyAssoc.java
===================================================================
--- ../ebean/src/com/avaje/ebean/server/deploy/BeanPropertyAssoc.java (revision 127)
+++ ../ebean/src/com/avaje/ebean/server/deploy/BeanPropertyAssoc.java Sat Feb 14 12:40:30 CET 2009
@@ -264,6 +264,11 @@
} else {
// embedded id
BeanPropertyAssocOne embProp = (BeanPropertyAssocOne)props[0];
+ if (embProp.getTargetDescriptor() == null)
+ {
+ // I'll consider this as a hack ...
+ embProp.initialise();
+ }
BeanProperty[] embBaseProps = embProp.getTargetDescriptor().propertiesBaseScalar();
ImportedIdSimple[] scalars = createImportedList(owner, cols, embBaseProps);

 
Rob 16 Feb 08:17
The issue here...

The issue here is:
- You have a entity bean with an EmbeddedId property
- It also has a OneToMany property

When BeanDescriptor initialises its properties ... it needs to make sure that it initialises the EmbeddedId property *BEFORE* the OneToMany property.

At the moment BeanDescriptor does not garuntee the order... and so you can get the NPE if the OneToMany property initialises before the EmbeddedId property.

Rob 16 Feb 09:26
I can reproduce

It looks to be as I thought. I can reproduce the NPE.

Exception in thread "main" java.lang.ExceptionInInitializerError
at main.FindCMaster.main(FindCMaster.java:14)
Caused by: java.lang.NullPointerException
at com.avaje.ebean.server.deploy.BeanPropertyAssocMany.createExported(BeanPropertyAssocMany.java:314)
at com.avaje.ebean.server.deploy.BeanPropertyAssocMany.initialise(BeanPropertyAssocMany.java:148)

The order of the properties is maintained so if you put the @EmbeddedId property AFTER the @OneToMany property in the java source code you will get this error - and moving the @EmbeddedId property above the @OneToMany in the java source code will get around this issue (you won't get the NPE).

@Entity
@Table(name="con_master")
public class CMaster {

String dcol;

@OneToMany(mappedBy="master")
List details;

@EmbeddedId
CMasterId id;

Rob 16 Feb 09:29
And the fix

And the fix is to The BeanDescriptor initialise() method:


// always initialise the Id properties first
BeanProperty[] idProps = propertiesId();
for (int i = 0; i < idProps.length; i++) {
idProps[i].initialise();
}

// now initialise all the non-id properties
Iterator it = propertiesAll();
while (it.hasNext()) {
BeanProperty prop = it.next();
//prop.initialise();
if (!prop.isId()){
prop.initialise();
}
}

Rob 16 Feb 09:31
Fixed in v1.0.3

Fixed in v1.0.3

Rob 17 Feb 11:12
Actually...

In testing with Bug66 etc ... this fix needed to change (as it didn't take into account imported properties).

So, the fix is now a 2 phase initialisation of the entity beans ... 1st phase initialises the ID properties and the second phase initialises the rest (including all the associated properties which need the imported and exported properties to already be initialised).

Rob 17 Feb 11:12
Still fixed in v1.0.3

Just to be clear - still fixed in v1.0.3 though.

woResponse

Upload a file