When I get a JobPosting entity returned, the user, province, country and employmentType fields, fields that are mapped with @OneToOne and are unidirectional, are populated with their id field but no other fields are populated, respectively. For example, the employmentType field is of type EmploymentType and has two fields; an id and type field. But the id field is only populated. I am using Hibernate core 3.3.2 GA, Hibernate Annotations and Entity Manager 3.4.0 GA, Struts 2.1.8 and Spring 2.5.6. How do I solve this problem?
package model;
import java.io.Serializable; import java.util.Date;
import javax.persistence.CascadeType; 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 org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode;
@Entity @NamedQueries({ @NamedQuery( name="findJobPostingsByUser", query="select jp from JobPosting jp where jp.user = :user" ), @NamedQuery( name="findJobPostingById", query="select jp from JobPosting jp where jp.id = :id" ) }) public class JobPosting implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue( strategy = GenerationType.AUTO ) private Long id; @OneToOne( fetch=FetchType.LAZY ) @JoinColumn( name="user_id" ) private User user; @Column( nullable=false, length=128 ) private String company; @Column( nullable=false ) private String jobTitle; @Column( nullable=false, length=8192 ) private String description; @Column( nullable=false ) private String city; @OneToOne( fetch=FetchType.LAZY ) @JoinColumn private Province province; @OneToOne( fetch=FetchType.LAZY ) @JoinColumn private Country country; private String payRate; @Column( nullable=false, length=2056 ) private String applyTo; @OneToOne( fetch = FetchType.LAZY ) @JoinColumn( nullable=false ) private EmploymentType employmentType; private Date createdAt; private Boolean active = true;
public Long getId() { return id; } public void setId(Long id) { this.id = id; }
@Override public int hashCode() { int hash = 0; hash += (id != null ? id.hashCode() : 0); return hash; }
@Override public boolean equals(Object object) { // TODO: Warning - this method won't work in the case the id fields are not set if( !( object instanceof JobPosting )) { return false; } JobPosting other = ( JobPosting )object; if(( this.id == null && other.id != null ) || ( this.id != null && !this.id.equals( other.id ))) { return false; } return true; }
@Override public String toString() { return "entity.JobPosting[id=" + id + "]"; }
public User getUser() { return user; } public void setUser( User user ) { this.user = user; }
public String getJobTitle() { return jobTitle; } public void setJobTitle( String jobTitle ) { this.jobTitle = jobTitle; }
public String getDescription() { return description; } public void setDescription(String description) { this.description = description; }
public String getCity() { return city; } public void setCity(String city) { this.city = city; }
public Province getProvince() { return province; } public void setProvince( Province province ) { this.province = province; }
public Country getCountry() { return country; } public void setCountry( Country country ) { this.country = country; } public Date getCreatedAt() { return createdAt; } public void setCreatedAt( Date createdAt ) { this.createdAt = createdAt; } public String getCompany() { return company; } public void setCompany( String company ) { this.company = company; } public String getApplyTo() { return applyTo; } public void setApplyTo( String applyTo ) { this.applyTo = applyTo; } public String getPayRate() { return payRate; } public void setPayRate( String payRate ) { this.payRate = payRate; } public EmploymentType getEmploymentType() { return employmentType; } public void setEmploymentType( EmploymentType employmentType ) { this.employmentType = employmentType; } public Boolean isActive() { return active; } public void setActive( Boolean active ) { this.active = active; } }
package model;
import java.io.Serializable; import java.lang.Integer; import java.lang.String; import javax.persistence.*;
@Entity @NamedQueries({ @NamedQuery( name="findAllEmploymentTypes", query="select et from EmploymentType et" ) }) public class EmploymentType implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String employmentType; private static final long serialVersionUID = 1L;
public EmploymentType() { super(); } public Integer getId() { return this.id; }
public void setId(Integer id) { this.id = id; } public String getEmploymentType() { return this.employmentType; }
public void setEmploymentType(String employmentType) { this.employmentType = employmentType; } }
|