5

I'm making an application in Java EE (Jersey) with JPA where I have a problem with the uninitialized entities. I have 3 entities Car, Owner, House where the car can have multiple owners and owners can have multiple houses. When i return (entityManager.find) Car then owner is initialized. When i return House then Owner is initialized, but Car is not. I would like to be able to call something like House.getOwner().getCar().getId(). Now I must call find on House and then call find on Owner to get Car. How do I resolve this?

@Entity
@Table(name = "House")
public class HouseEntity {

  @Id
  @Column(name = "id")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;

  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "owner_id", nullable = false)
  private OwnerEntity owner;
}

@Entity
@Table(name = "Owner")
public class OwnerEntity {

  @Id
  @Column(name = "id")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;

  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "car_id")
  private CarEntity car;

  @OneToMany(mappedBy = "owner")
  @JoinColumn(name = "house", nullable = false)
  private Set<HouseEntity> house;

}

@Entity
@Table(name = "Car")
public class CarEntity {

  @Id
  @Column(name = "id")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;

  @OneToMany(mappedBy = "owner")
  private Set<OwnerEntity> owner;
}

Edit1: Sorry there was mistake in mapping, classes working well. But problem with initialization remains.

user1816532
  • 185
  • 4
  • 13
  • try use Hibernate.initialize() – Exorcismus Oct 25 '14 at 09:57
  • Something wrong here : OwnerEntity maps itself with Set owner ? Isn't it Set houses? – nomoa Oct 25 '14 at 10:01
  • Yes I made mistake there. I these entities are big so i edited them to be shorter but mistake happened. – user1816532 Oct 25 '14 at 10:12
  • In CarEntity the owner mappedBy is also wrong, shoud be mappedBy = "car" – nomoa Oct 25 '14 at 10:13
  • Except typo errors that would prevent hibernate to map the entities, I see no problem in the mapping. Problem is elsewhere, did you checked that the getter and setter method names match attribute names ? – nomoa Oct 25 '14 at 10:24

1 Answers1

0

Try this:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "House")
public class HouseEntity {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToOne
    @JoinColumn(name = "owner_id", nullable = false)
    private OwnerEntity owner;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public OwnerEntity getOwner() {
        return owner;
    }

    public void setOwner(OwnerEntity owner) {
        this.owner = owner;
    }

}





import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "Owner")
public class OwnerEntity {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToOne
    @JoinColumn(name = "car_id")
    private CarEntity car;

    @OneToMany(mappedBy = "owner", fetch = FetchType.EAGER)
    private Set<HouseEntity> houses;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public CarEntity getCar() {
        return car;
    }

    public void setCar(CarEntity car) {
        this.car = car;
    }

    public Set<HouseEntity> getHouses() {
        return houses;
    }

    public void setHouses(Set<HouseEntity> houses) {
        this.houses = houses;
    }

}



import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "Car")
public class CarEntity {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @OneToMany(mappedBy = "car", fetch = FetchType.EAGER)
    private Set<OwnerEntity> owner;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Set<OwnerEntity> getOwner() {
        return owner;
    }

    public void setOwner(Set<OwnerEntity> owner) {
        this.owner = owner;
    }

    public void addOwner(OwnerEntity owner){
        this.owner.add(owner);
    }

}

When you fetch for Car entire object graph will be populated.

jithin iyyani
  • 751
  • 1
  • 7
  • 19