Hi all,
I'm trying to resolve my LazyInitializationException's by always fetching eagerly. (and because Lazy Loading doesn't seem to work when sending objects from Java to Flex via BlazeDS)
I've got a bidirectional relationship between my Person and Vehicle classes:
Person with OneToMany to Vehicle:Code:
@Entity
@Table(name = "person")
public class Person {
//Fields
private Long id;
private String name;
private Date birthDate;
private Set<Vehicle> vehicles = new HashSet<Vehicle>();
//Property accessors
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
System.out.println("in java Person-> setId = "+id);
this.id = id;
}
@Column(name = "Name", nullable = false, length = 35)
public String getName() {
return this.name;
}
public void setName(String name) {
System.out.println("in java Person-> setName = "+name);
this.name = name;
}
@Temporal(TemporalType.DATE)
@Column(name = "BirthDate", nullable = true, length = 10)
public Date getBirthDate() {
return this.birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
@CollectionOfElements(targetElement = be.tradelec.model.Vehicle.class)
@OneToMany(mappedBy = "person_id", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
public Set<Vehicle> getVehicles() {
return vehicles;
}
public void setVehicles(Set<Vehicle> vehicles) {
System.out.println("in java Person-> setVehicles = "+vehicles);
this.vehicles = vehicles;
}
public void addVehicle(Vehicle vehicle) {
vehicles.add(vehicle);
vehicle.setPerson(this);
}
}
Vehicle with ManyToOne to Peron:Code:
@Entity
@Table(name = "vehicle")
public class Vehicle {
// Fields
private Long id;
private Long person_id;
private Person person;
private String licensePlate;
private String brand;
private String type;
// Constructors
/** default constructor */
public Vehicle() {
}
/** full constructor */
public Vehicle(Long id, Person person,
String licensePlate, String brand, String type) {
this.id = id;
//this.person = person;
this.licensePlate = licensePlate;
this.brand = brand;
this.type = type;
}
// Property accessors
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
System.out.println("in java Vehicle-> setIdVehicle = "+id);
this.id = id;
}
@CollectionOfElements(targetElement = be.tradelec.model.Person.class)
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "person_id")
public Person getPerson() {
return this.person;
}
public void setPerson(Person person) {
this.person = person;
}
@Column(name = "LicensePlate", nullable = true, length = 10)
public String getLicensePlate() {
return this.licensePlate;
}
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
@Column(updatable=false, insertable=false)
public Long getPerson_id() {
return person_id;
}
public void setPerson_id(Long person_id) {
this.person_id = person_id;
}
@Column(name = "Brand", nullable = false, length = 45)
public String getBrand() {
return this.brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
@Column(name = "Type", nullable = false, length = 45)
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
}
Saving a Person with added Vehicles works nicely. And with standard fetchtypes I can load my Person (with his vehicles) perfectly.
Now when I try to switch both my mappings to EAGER fetchtypes so I can send the whole Object graph through BlazeDS, I get an error while loading:
Code:
Hibernate: select person0_.id as id4_1_, person0_.BirthDate as BirthDate4_1_, person0_.Name as Name4_1_, vehicles1_.person_id as person4_3_, vehicles1_.id as id3_, vehicles1_.id as id5_0_, vehicles1_.Brand as Brand5_0_, vehicles1_.LicensePlate as LicenseP3_5_0_, vehicles1_.person_id as person4_5_0_, vehicles1_.Type as Type5_0_ from person person0_ left outer join vehicle vehicles1_ on person0_.id=vehicles1_.person_id where person0_.id=?
in java Vehicle-> setIdVehicle = 1
in java Person-> setId = 1
in java Vehicle-> setIdVehicle = 2
in java Person-> setName = Jochen
17-jun-2009 10:30:06 org.hibernate.LazyInitializationException <init>
SEVERE: illegal access to loading collection
org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:363)
...
I've found similar threads but never could use that info to solve my issue here:
viewtopic.php?f=1&t=969044&p=2335298&hilit=illegal+access+to+loading+collection#p2335298viewtopic.php?f=1&t=991432&hilit=+illegal+access+to+loading+collection+Thanks in advance, all comments more than welcomem!
Jochen