[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-7073) Audited entities with Many-to-Many relationships fail with NullPointerException
Clement Pang (JIRA)
noreply at atlassian.com
Wed Feb 22 01:04:09 EST 2012
[ https://hibernate.onjira.com/browse/HHH-7073?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=45578#comment-45578 ]
Clement Pang commented on HHH-7073:
-----------------------------------
Thanks for the help. Here are the two entities.
{code:title=Apple.java}
@Entity
@Audited
public class Apple {
@Id
@GeneratedValue
public long id;
@ManyToMany(cascade = {CascadeType.PERSIST})
@JoinTable(
name = "ApplesAndOranges",
joinColumns = {@JoinColumn(name = "appleId", nullable = false)},
inverseJoinColumns = {@JoinColumn(name = "orangeId", nullable = false)}
)
public Set<Orange> oranges = new HashSet<Orange>();
}
{code}
{code:title=Orange.java}
@Entity
@Audited
public class Orange {
@Id
@GeneratedValue
public long id;
@ManyToMany(cascade = {CascadeType.PERSIST})
public Set<Apple> apples = new HashSet<Apple>();
}
{code}
The code fails when calling:
{code}
Orange orange = new Orange();
entityManager.persist(orange);
{code}
We have also tried:
{code:title=Orange.java}
@Entity
@Audited
public class Orange {
@Id
@GeneratedValue
public long id;
@ManyToMany(cascade = {CascadeType.PERSIST})
@NotAudited
public Set<Apple> apples = new HashSet<Apple>();
}
{code}
and:
{code:title=Orange.java}
@Entity
@Audited
public class Orange {
@Id
@GeneratedValue
public long id;
@ManyToMany(mappedBy = "oranges", cascade = {CascadeType.PERSIST})
@NotAudited
public Set<Apple> apples = new HashSet<Apple>();
}
{code}
Again, the issue seems to be the fact that RelationDescription can have null mappedByPropertyName when the RelationType is TO_MANY_MIDDLE.
Note that the exact same code works in Hibernate 3.0. We are migrating to 4.0 and it started failing.
> Audited entities with Many-to-Many relationships fail with NullPointerException
> -------------------------------------------------------------------------------
>
> Key: HHH-7073
> URL: https://hibernate.onjira.com/browse/HHH-7073
> Project: Hibernate ORM
> Issue Type: Bug
> Components: envers
> Affects Versions: 4.1.0
> Reporter: Clement Pang
>
> The entity contains the following field:
> @ManyToMany(cascade = {CascadeType.PERSIST})
> @JoinTable(
> name = "Apples",
> joinColumns = {@JoinColumn(name = "storeId", nullable = false)},
> inverseJoinColumns = {@JoinColumn(name = "appleId", nullable = false)}
> )
> public Set<Apple> apples = new HashSet<Apple>();
> An exception is thrown when saving the entity:
> java.lang.NullPointerException
> at org.hibernate.envers.entities.EntitiesConfigurations.getToPropertyNames(EntitiesConfigurations.java:155)
> at org.hibernate.envers.event.BaseEnversEventListener.addCollectionChangeWorkUnit(BaseEnversEventListener.java:121)
> at org.hibernate.envers.event.BaseEnversEventListener.generateBidirectionalCollectionChangeWorkUnits(BaseEnversEventListener.java:90)
> at org.hibernate.envers.event.EnversPostInsertEventListenerImpl.onPostInsert(EnversPostInsertEventListenerImpl.java:60)
> at org.hibernate.action.internal.EntityIdentityInsertAction.postInsert(EntityIdentityInsertAction.java:149)
> at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:102)
> In that particular method:
> public Set<String> getToPropertyNames(String fromEntityName, String fromPropertyName, String toEntityName) {
> Set<String> entityAndParentsNames = getEntityAndParentsNames(fromEntityName);
> Set<String> toPropertyNames = new HashSet<String>();
> for (RelationDescription relationDescription : getRelationDescriptions(toEntityName)) {
> String relToEntityName = relationDescription.getToEntityName();
> String mappedByPropertyName = relationDescription.getMappedByPropertyName();
> if (entityAndParentsNames.contains(relToEntityName) && mappedByPropertyName.equals(fromPropertyName)) {
> toPropertyNames.add(relationDescription.getFromPropertyName());
> }
> }
> return toPropertyNames;
> }
> mappedByPropertyName is null since RelationDescription can have null mappedByPropertyName when invoked at: EntityConfiguration
> public void addToManyMiddleRelation(String fromPropertyName, String toEntityName) {
> relations.put(fromPropertyName, new RelationDescription(fromPropertyName, RelationType.TO_MANY_MIDDLE,
> toEntityName, null, null, null, null, true));
> }
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the hibernate-issues
mailing list