-->
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.  [ 1 post ] 
Author Message
 Post subject: Embedded-id JPA/Hibernate, loop and stackoverflow loading!
PostPosted: Wed Mar 28, 2007 10:18 am 
Newbie

Joined: Wed Mar 28, 2007 10:07 am
Posts: 1
Lets assume I have the follow, and also assume that
it has all the getters and setters and I have overwritten
the hashCode and equals methods.


Code:
@Embeddable
public class CarNameKey implements Serializable {

    private static final long serialVersionUID = -8069585143246555735L;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CAR_ID")
    private Car car;

    @Column(name = "NAME_TYPE")
    private Integer nameType;
}

@Entity
public class CarName implements Serializable {

    private static final long serialVersionUID = -5035851532232357351L;

    @EmbeddedId
    private CarNameKey carNameKey;

    @Column(name = "CAR_NAME", nullable = false, length = 64)
    private String carName;

    @Column(name = "CAR_NAME_ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CAR_NAME_SEQ")
    @SequenceGenerator(name = "CAR_NAME_SEQ", sequenceName = "CAR_NAME_SEQ")
    private Integer carNameId;
}

@Entity
public class Car implements Serializable, Validatable, Cloneable {

    private static final long serialVersionUID = -6447379549417424224L;

    @Id
    @Column(name = "CAR_ID", nullable = false, length = 12)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CAR_SEQ")
    @SequenceGenerator(name = "CAR_SEQ", sequenceName = "CAR_SEQ", allocationSize = 1)
    private Integer carId;

    @OneToMany(mappedBy = "carNameKey.car", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
    private Set<CarName> carNames;
}


Now, if I do a load on the entity manager on the entity of type CAR
with an id of say 5, it should traverse down the elements and load
the set of car names if any.

And as expected, it does! However the ManyToOne relationship
of Car in composite key CarNameKey is set to fetchtype lazy!

If I change the embedded key object carNameKey.car to use FetchType.EAGER on instance of "Car", it will fall into a recursive
loop.

Code:
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "CAR_ID")
    private Car car;


Now, I imagine this is because the entity manager fails to see that the object has already been loaded? As such it go through a flow of repeatedly loading entity of Car -> Car Names (compsite-key) -> Key.Car (again)

I assume this is caused by the hashCode and equals methods have
not been over written. So, I implemented a hashCode and equals method
with the @Override annotation, the problem still persists! It falls into a
close to never ending loop followed by either a stackoverflow or
not enough memory exception.

-- Interesting enough, I checked the unit test in debug mode and put several breakpoints on the equals and hashCode methods within each of the classes, hibernate doesn't seem to be calling them at all !
it goes into a loop and keeps pulling the database.


Any suggestions ?

_________________
Kaz-


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

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.