-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Problem with SINGLE_TABLE inheritance.
PostPosted: Tue Mar 29, 2011 5:54 am 
Newbie

Joined: Thu Aug 20, 2009 11:25 am
Posts: 6
Hi.

I am using Hibernate 3.5.1 with JPA.

I have the following class structure. Getters, Setters, Properties, Methods ommited for clarity.

Code:
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "ORGANISATION_DISCRIMINATOR", discriminatorType = DiscriminatorType.INTEGER, length = 1)
@ForceDiscriminator
public abstract class Organisation() {
...
}

@Entity
@DiscriminatorValue("1")
public class Area extends Organisation {
    @OneToMany(mappedBy = "area", cascade = CascadeType.ALL)
    private List<Division> divisions;
}

@Entity
@DiscriminatorValue("2")
public class Division extends Organisation {
    @ManyToOne
    @JoinColumn(name = "PARENT_ID")
    private Area area;

    @OneToMany(mappedBy = "division", cascade = CascadeType.ALL)
    private List<Team> teams;
}

@Entity
@DiscriminatorValue("3")
public class Team extends Organisation {
    @ManyToOne
    @JoinColumn(name = "PARENT_ID")
    private Division division;
}


These are the only classes in my system which use the SINGLE_TABLE mechanism, and it has been designed this way for a specific purpose.

I have a strange problem where the fetching of entities only works SOME of the time.

If I call:

this.entityManager.find(Organisation.class, organisationId);

Some of the time it works, and the rest of the time I get:

javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not set a field value by reflection setter of entity.jpa.Division.area.

I thought the ForceDiscriminator would solve it as I had found in some previous posts, but it has not.

I just do not understand why it only fails some of the time. Is it an issue with the entities being cached in a certain way maybe? I know the SINGLE_TABLE mechanism is not ideal, but it is in the design for a reason and I would like to try and resolve it as best I can.

Many Thanks

Chris


Top
 Profile  
 
 Post subject: Re: Problem with SINGLE_TABLE inheritance.
PostPosted: Tue Mar 29, 2011 10:53 am 
Newbie

Joined: Thu Aug 20, 2009 11:25 am
Posts: 6
I have changed all my mappings within the single table mechanism to use Eager loading and I no longer get any complains. I needed to do this both ways.

So within the middle tier. I have need to add eager to the parent Area and also the child Teams

Is there something fundamental I am missing?

Thanks

Chris


Top
 Profile  
 
 Post subject: Re: Problem with SINGLE_TABLE inheritance.
PostPosted: Fri Apr 01, 2011 7:49 am 
Newbie

Joined: Thu Aug 20, 2009 11:25 am
Posts: 6
Update. This is still not working. There seems to be some issue where it works sometimes and fails on other times with absolutely no pattern at all. I do not know if this is something to do with fetching cached objects against fetching fresh objects.

Can anyone help?


Top
 Profile  
 
 Post subject: Re: Problem with SINGLE_TABLE inheritance.
PostPosted: Fri Apr 01, 2011 9:55 am 
Newbie

Joined: Thu Aug 20, 2009 11:25 am
Posts: 6
Code:
    @OneToMany(mappedBy = "force", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Where(clause="ORGANISATION_DISCRIMINATOR = 2")   
    private List<Area> areas;


I have also tried the Hibernate @Where method but I still get this exception:

could not set a field value by reflection setter of x.x.data.entity.jpa.Division.area


Top
 Profile  
 
 Post subject: Re: Problem with SINGLE_TABLE inheritance.
PostPosted: Tue Apr 05, 2011 5:43 am 
Newbie

Joined: Thu Aug 20, 2009 11:25 am
Posts: 6
If I fetch an Area, all the Divisions and Teams underneath are populated, I am assuming because each child is fetched seperately from the parent. However the problem appears when I try to cleanly fetch a single Division child and it fails on fetching the parent specified by the ManyToOne. I am assuming this is because the parent is fetched with a join to the child.

Code:
@Entity
@Table(name = "ORGANISATION")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "ORGANISATION_DISCRIMINATOR", discriminatorType = DiscriminatorType.INTEGER, length = 1)
@ForceDiscriminator
public abstract class Organisation() {
...
}

@Entity
@DiscriminatorValue("1")
public class Area extends Organisation {
    @OneToMany(mappedBy = "area", cascade = CascadeType.ALL)
    private List<Division> divisions;
}

@Entity
@DiscriminatorValue("2")
public class Division extends Organisation {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PARENT_ID")
    private Area area;

    @OneToMany(mappedBy = "division", cascade = CascadeType.ALL)
    private List<Team> teams;
}

@Entity
@DiscriminatorValue("3")
public class Team extends Organisation {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PARENT_ID")
    private Division division;
}


I have managed to get this working by specifying lazy loading for the ManyToOne specifying the parent relationship. So I can now fetch a Division and the Area is fetched seperately and the problem does not happen.

Can anyone help me understand the reason for this?

Thanks

Chris


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.