I have a problem I got a repeated rows in the parent entity as much as I insert a child in the database (e.g. if I insert 3 child to the same parent i got the parent repeated 3 times in the list returns from database) I use InheritanceType = TABLE_PER_CLASS.
the Enterprise entity
Code:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Enterprise {
private Set<Contact> contact;
private Integer id;
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy = "enterprise",targetEntity=Contact.class)
public Set<Contact> getContact() {
return contact;
}
@Id
@GeneratedValue(generator="idGen")
@GenericGenerator(name="idGen",strategy="org.hibernate.id.IncrementGenerator")
@Column(name = "ID", unique = true, nullable = false)
public Integer getId() {
return id;
}
public void setContact(Set<Contact> contact) {
this.contact = contact;
}
public void setId(Integer id) {
this.id = id;
}
}
And the contact entity
Code:
Entity
@Table(name = "CONTACT")
public class Contact implements Serializable {
private Enterprise enterprise;
private Integer id;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
public Integer getId() {
return id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ENTERPRISE_ID")
public Enterprise getEnterprise() {
return enterprise;
}
public void setEnterprise(Enterprise enterprise) {
this.enterprise = enterprise;
}
}
And the Vendor entity
Code:
@Entity
@Table(name = "VENDOR")
public class Vendor extends Enterprise implements java.io.Serializable {
----------------
}
I use Hibernate and Spring With JSF.
Code:
public List<T> findAll(T type) {
// TODO Auto-generated method stub
return getHibernateTemplate().loadAll((Class<T>) type);
}
T is the Vendor entity.
the returned list contain a repeated parents as many as its child