0

I am using hibernate for database communication. I have one class as:

@Table(name="Person")
public class Person {

@Column(name="name")
private String name;

@OneToMany
@JoinColumn(name="Address_id)
private Set<Address> address;

... <other filed similarly>

}

Now I want to get this object using its primary key , but object should have only specific columns populate?

I tried using criteria and projection, it is returning a result but it is not mapped to Object i expect (Person Object)

Any idea how to solve this problem using hibernate query/criteria?

Thanks

Kailas
  • 807
  • 1
  • 6
  • 20

1 Answers1

0

The hibernate annotations used have to be correctly written.

Let assume that you have two Entities: Person and Address.

 @Table(name="Person")
public class Person {

@Column(name="name")
private String name;

@OneToMany
@JoinColumn(name="Address_id")
private Set<Address> address;

... <other filed similarly>

}

And on the other side you have Address class

@Table(name="addresses")
public class Address{

@Column(name="name")
private String addressName;
@ManyToOne
private Person person;

}

Using this mapping when you have a method like:

 public Encounter getAddressById(int idAddress) {
    session = sf.getCurrentSession();
    session.beginTransaction();

  Address address = (Address ) session.load(Address .class, idAddress);

    return address ;
}

This should return the address mapped with person; and the display of some columns will choosen by you because hibernate here returned the entire object.

user1718215
  • 61
  • 1
  • 7