Thanks tseringshrestha, but let me show you my mappings:
The actual names are Pessoa for Person and Telefone for Phone (sorry, I translated before)
Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Pessoa {
@Id
@SequenceGenerator(name="pessoa_seq", sequenceName="PESSOA_SEQ", allocationSize=1)
@GeneratedValue(strategy=GenerationType.AUTO, generator="pessoa_seq")
private Long id;
@OneToMany(mappedBy="pessoa", fetch=FetchType.EAGER, cascade=CascadeType.ALL) // At this time, I want Eager fetching
private Set<Telefone> telefones;
public Pessoa(){
}
// Getters and setters
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pessoa other = (Pessoa) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
Code:
@Entity
public class Telefone {
@Id
@SequenceGenerator(name="telefone_seq", sequenceName="TELEFONE_SEQ", allocationSize=1)
@GeneratedValue(strategy=GenerationType.AUTO, generator="telefone_seq")
private Long id;
@ManyToOne
private Pessoa pessoa;
// Getters and setters
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Telefone other = (Telefone) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
Then I have this code on my DAO
Code:
@SuppressWarnings("unchecked")
@Override
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public List<T> findByExample(T entity) {
Example example = Example.create(entity).ignoreCase().enableLike(MatchMode.ANYWHERE);
return getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(entity.getClass()).add(example).list();
}
Now trying to create a Pessoa, and add two Telefones, and persist them, I run my findByExample method with new Pessoa() as a parameter, and I have 2 rows on my SQL being returned, instead of one row like the query find("* pessoa"). My findByExample made a left outer join on table Telefone, but I don't want this to happen!
Thanks in advance.