• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Associated objects in Hibernate findByExample is not working for associated objects s

 
Venkat Pingala
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am working on Hibernate and Spring Dao getting some problem when accessing associated objects.

I am tring to search Objects in hibernate by using findByExample it is working fine, when i gave straight objects.
I am unable to search associated objects, that time it is returning all list in database.
 
Kuladip Yadav
Ranch Hand
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am tring to search Objects in hibernate by using findByExample it is working fine, when i gave straight objects.
I am unable to search associated objects, that time it is returning all list in database.


How do you get results by passing straight objects ?
Will you please paste code here ?
 
Venkat Pingala
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my code
*********************************************************************************
public List<T> findByExample(T object) {

List<T> list = new ArrayList<T>();

list = getHibernateTemplate().findByExample(object);

return list;
***********************************************************************************
when i call

Zone zone = new Zone();

zone.setName("Abc");

List<Zone> list = zoneDao.findByExample(zone);

It is working fine


when i call with associated objects its working, it is returning all data in Zone table

like below
Zone zone = new Zone();
Status status = new Status();
status.setOId(200L);
zone.setStatus(status);
List<Zone> list = zoneDao.findByExample(zone); // here its returning all the records in database

here is my Zone Pojos

***************************************************************************************************

import java.io.Serializable;

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.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;


@Entity
@Table(name = "ZONE")
@NamedQueries ({
@NamedQuery(
name = "findZoneByName",
query = "select z from Zone z where z.zoneName=:name "
) })

public class Zone extends BaseObject implements Serializable {

private static final long serialVersionUID = 748963981490193262L;
private String zoneName;
private Long zoneId;
private String zoneCode;
private Status status;
private Long externalId;

public Zone() {
}

public Zone(String zoneName) {
setZoneName(zoneName);
}

@Column(name = "NAME")
public String getZoneName() {
return zoneName;
}

public void setZoneName(String zoneName) {
this.zoneName = zoneName;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ZONEID")
public Long getZoneId() {
return zoneId;
}

public void setZoneId(Long zoneId) {
this.zoneId = zoneId;
}

@Column(name = "ZONECODE")
public String getZoneCode() {
return zoneCode;
}

public void setZoneCode(String zoneCode) {
this.zoneCode = zoneCode;
}
@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="STATUSID")
@Fetch(FetchMode.JOIN)
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}

@Column(name="externalId",unique=true)
public Long getExternalId() {
return externalId;
}

public void setExternalId(Long externalId) {
this.externalId = externalId;
}

******************************************************************************************************************
Status Pojo

@Entity
@Table(name="STATUS")
public class Status extends BaseObject implements Serializable {


/**
*
*/
private static final long serialVersionUID = -7618067311891699881L;
private Long oId;
private String name;
private String desription;
private String type;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="OID")
public Long getOId() {
return oId;
}
public void setOId(Long id) {
oId = id;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="DESCRIPTION")
public String getDesription() {
return desription;
}
public void setDesription(String desription) {
this.desription = desription;
}
@Column(name="TYPE")
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic