-->
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.  [ 6 posts ] 
Author Message
 Post subject: Inconsistent behaviour when fetching cached subclass entitie
PostPosted: Thu Feb 25, 2010 6:11 am 
Newbie

Joined: Thu Feb 25, 2010 5:25 am
Posts: 1
Hi all,

Enabling the hibernate second-level cache is leading to inconsistent behaviour when fetching cached subclass entities as opposed to fetching non-cached subclass entities.

Consider a simple scenario having a parent entity:
Code:
@Entity
@Table(name = "tt_test")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class ParentEntity implements Serializable {
   
    private static final long serialVersionUID = 5751958972675892020L;
   
    @Id
    private String id;
   
    protected ParentEntity() {
        super();
    }
   
    public ParentEntity(String id) {
        this.id = id;
    }
   
    public String getId() {
   
        return id;
    }
   
}


and two subclass entities extending the above entity:
Code:
@Entity
public final class Subclass1 extends ParentEntity {

    private static final long serialVersionUID = -4329753920470094896L;

    protected Subclass1() {
        super();
    }
   
    public Subclass1(String id) {
        super(id);
    }

    @Override
    public String toString() {

        return "Subclass1 [ID={" + getId() + "}, Class={" + getClass() + "}]";
    }
}


Code:
@Entity
public final class Subclass2 extends ParentEntity {

    private static final long serialVersionUID = -6102575763486357670L;

    /**
     * Default constructor (required for hibernation reasons)
     */
    protected Subclass2() {
        super();
    }
   
    public Subclass2(String id) {
        super(id);
    }
   

    @Override
    public String toString() {

        return "Subclass2 [ID={" + getId() + "}, Class={" + getClass() + "}]";
    }
}


After persisting an instance of Subclass1 using:
Code:
em.persist(new Subclass1("identifier1"));


Trying to fetch an instance of Subclass2 using:
Code:
em.find(Subclass2.class, "identifier1");

will result in a null return if the entity is not cached. This is the expected, correct behaviour.

However, when the entity with id identifier1 is cached, this will match and return an instance (of Subclass1) if the entity is cached. This appears to be both inconsistent behaviour, and a wrong result, as the entity with identifier identifier1 is of type Subclass1 not SubClass2, as requested in the call to em.find().

This behaviour was reproduced in the following environements:

jboss-cache 1.4.1.SP13
hibernate 3.2.5
jboss 4.2.2

EHCache - terracotta-3.2.0
hibernate 3.2.5
jboss 4.2.2

jboss-cache 3.1.0
hibernate 3.3.1.GA
jboss 5.1.0.GA

As this was reproducible on both jboss-cache and EHCache it seems to be a hibernate second level caching problem.

Any help on this matter will be very much appreciated.


Top
 Profile  
 
 Post subject: Re: Inconsistent behaviour when fetching cached subclass entitie
PostPosted: Thu Feb 25, 2010 7:30 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
This is definitely a bug and it happens also with Hibernate3.5CR2.
I will open a issue on Hibernate if there's is not already one for this topic.
I will let you know...


Top
 Profile  
 
 Post subject: Re: Inconsistent behaviour when fetching cached subclass entitie
PostPosted: Thu Feb 25, 2010 9:11 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
http://opensource.atlassian.com/projects/hibernate/browse/HHH-4953

Please vote for it


Top
 Profile  
 
 Post subject: Re: Inconsistent behaviour when fetching cached subclass entitie
PostPosted: Wed Mar 10, 2010 5:59 pm 
Newbie

Joined: Tue Aug 12, 2008 12:59 pm
Posts: 6
Location: Massachusetts
I am going to attempt to clarify the issue we are having (I work with Nathan, who posted the issue).

The actual issue is that hibernate is showing the wrong subclass type when data is retrieved.

We are able to add the data correctly and if we look in the database it shows the correct type but when we query the table it shows all of the values with the same subclass type even though they are actually different.

The issue only appears to affect a subclass within a subclass.

Here is a scenario similar to what we are trying to do. I will use vehicles to make it easier to follow.

We have an abstract class of Vehicle that has subclasses (Truck, Car, Motorcycle).

We have an abstract class of VehicleType that has subclasses (TruckType, CarType, MotorcycleType).

Data in the VehicleType table:
ID Discriminator Description
1 CarType Chevrolet
2 CarType Buick
3 CarType Toyota
4 MotorcycleType Harley Davidson
5 MotorcycleType Honda
6 TruckType Mack
7 TruckType GMC

Data in the Vehicle table:
ID Discriminator Serial number VehicleType
101 Motorcycle 654321 4
102 Car 123457 3
103 Truck 321456 7
104 Motorcycle 654123 5
105 Car 123456 1

If we query the entire VehicleType table we get 7 rows and each row shows the class as being the correct subtype.

If we query the entire Vehicle table we get 5 rows and each vehicle shows as the correct subclass. The problem is that when you look at the type WITHIN each row they ALL show as subclass of CarType, even though they show the correct type ID (vehicle 101 has type 4, vehicle 102 has type 3, etc.).

We have tried this with both eager and lazy loading. With eager loading an error occurs when you run the query. With lazy loading the query runs fine but the error occurs when you attempt to access the vehicle type.

In both cases the error is an EntityNotFoundException. If we try to retrieve vehicle id 101 we get the exception with a message of "Unable to find xxx.xxx.Car with id of 4". If you look in the database vehicle 101 has a type ID of 4 and if you look at the vehicle type table id 4 is a type of motorcycle (which is correct). For some reason it appears that hibernate is always expecting the vehicle type to be a CarType subclass regardless of what the data in the table is.

Here is an example of what the class definitions would look like:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Vehicle<T extends VehicleType> extends BaseEntity {

@Column(nullable = false, columnDefinition = "INT(11) UNSIGNED")
private Integer id;

@Column(nullable = false, columnDefinition = "INT(11) UNSIGNED")
private Integer serialNumber;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "typeID", nullable = false)
private T vehicleType;
}

@Entity
public class Car extends Vehicle {
}

@Entity
public class Motorcycle extends Vehicle {
}

@Entity
public class Truck extends Vehicle {
}

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class VehicleType extends BaseEntity {

@Column(nullable = false, columnDefinition = "INT(11) UNSIGNED")
private Integer id;

@Column(nullable = false, columnDefinition = "INT(11) UNSIGNED")
private String description;
}

@Entity
public class CarType extends VehicleType {
}

@Entity
public class MotorcycleType extends VehicleType {
}

@Entity
public class TruckType extends VehicleType {
}

I hope this clarifies the issue we are having and that someone has a solution to this issue.


Top
 Profile  
 
 Post subject: Re: Inconsistent behaviour when fetching cached subclass entitie
PostPosted: Wed Mar 10, 2010 6:09 pm 
Newbie

Joined: Tue Aug 12, 2008 12:59 pm
Posts: 6
Location: Massachusetts
I accidentally posted my previous message to the wrong thread.

Feel free to disregard my post (or to answer it if you have a solution).


Top
 Profile  
 
 Post subject: Re: Inconsistent behaviour when fetching cached subclass entitie
PostPosted: Wed Jul 06, 2016 3:13 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
This issue is resolved with HHH-9107 with the decision to raise an exception in both cases (with 2nd-level-cache enabled and without)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.