[jboss-jira] [JBoss JIRA] Updated: (JBRULES-2522) JPAVariablePersister will not work when @Id is declared on a superclass
Julien Serdaru (JIRA)
jira-events at lists.jboss.org
Thu May 27 09:45:08 EDT 2010
[ https://jira.jboss.org/browse/JBRULES-2522?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Julien Serdaru updated JBRULES-2522:
------------------------------------
Description:
Given A and B:
@MappedSuperclass
public class A {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id = null;
}
@Entity
public class B extends A {
// Some other fields
}
In JPAVariablePersister:getClassIdValue, a lookup is done on the entity fields to autodetect the @Id annotation, which fails in this case since the annotation is declared on the superclass.
I tested the following implementation of getClassIdValue to work:
private Long getClassIdValue(Object o)
throws NoSuchMethodException, SecurityException,
IllegalAccessException, InvocationTargetException,
IllegalArgumentException {
Class<?> c = o.getClass();
Long idValue = null;
do {
Field fields[] = c.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Id id = fields[i].getAnnotation(Id.class);
if (id != null) {
idValue = (Long) o.getClass().getMethod(
"get"
+ Character.toUpperCase(fields[i]
.getName().charAt(0))
+ fields[i].getName().substring(1),
new Class<?>[] {}).invoke(o,
new Object[] {});
break;
}
}
} while ((c = c.getSuperclass()) != null && idValue == null);
return idValue;
}
was:
Given A and B:
@MappedSuperclass
public class A {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id = null;
}
@Entity
public class B extends A {
// Some other fields
}
In JPAVariablePersister:getClassIdValue, a lookup is done on the entity fields to autodetect the @Id annotation, which fails in this case since the method uses getDeclaredFields() instead of getFields():
private Long getClassIdValue(Object o) throws NoSuchMethodException,
SecurityException, IllegalAccessException,
InvocationTargetException, IllegalArgumentException {
Field[] fields = o.getClass().getDeclaredFields(); <-- THIS ONLY RETURNS THE LOCAL FIELDS, NOT THE SUPERCLASS.
...
return idValue;
}
Replacing getDeclaredFields() by getFields() should fix the problem.
> JPAVariablePersister will not work when @Id is declared on a superclass
> -----------------------------------------------------------------------
>
> Key: JBRULES-2522
> URL: https://jira.jboss.org/browse/JBRULES-2522
> Project: Drools
> Issue Type: Bug
> Security Level: Public(Everyone can see)
> Affects Versions: 5.1.0.M1
> Reporter: Julien Serdaru
> Assignee: Mark Proctor
>
> Given A and B:
> @MappedSuperclass
> public class A {
> @Id
> @GeneratedValue(strategy = GenerationType.AUTO)
> private Long id = null;
> }
> @Entity
> public class B extends A {
> // Some other fields
> }
> In JPAVariablePersister:getClassIdValue, a lookup is done on the entity fields to autodetect the @Id annotation, which fails in this case since the annotation is declared on the superclass.
> I tested the following implementation of getClassIdValue to work:
> private Long getClassIdValue(Object o)
> throws NoSuchMethodException, SecurityException,
> IllegalAccessException, InvocationTargetException,
> IllegalArgumentException {
> Class<?> c = o.getClass();
> Long idValue = null;
> do {
> Field fields[] = c.getDeclaredFields();
> for (int i = 0; i < fields.length; i++) {
> Id id = fields[i].getAnnotation(Id.class);
> if (id != null) {
> idValue = (Long) o.getClass().getMethod(
> "get"
> + Character.toUpperCase(fields[i]
> .getName().charAt(0))
> + fields[i].getName().substring(1),
> new Class<?>[] {}).invoke(o,
> new Object[] {});
> break;
> }
> }
> } while ((c = c.getSuperclass()) != null && idValue == null);
> return idValue;
> }
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: https://jira.jboss.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the jboss-jira
mailing list