Hi, all.
I'm getting 4 items in result of
Code:
criteria.list()
instead of expected one.
Has anyone faced the same issue?
Looks like Hibernate issue.
If
FetchType is set to
LAZY of
@ElementCollection fetch strategy it works as expected.
I'm getting as many items as items in extraProperties.
Sample code is provided.
Code:
@Entity
@Table(name = "source")
public class Source implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "extraData_fk")
private ExtraData extraData;
// skipped other code
}
Code:
@Table(name = "extra_data")
public class ExtraData implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;
@ElementCollection(fetch = FetchType.EAGER)
@JoinTable(name = "extra_data_properties", joinColumns = @JoinColumn(name = "extra_data_id"))
@MapKeyColumn(name = "pname")
@Column(name = "pvalue")
private Map<String, String> extraProperties;
// skipped other code
}
Code:
public class HibernateTest {
public static void main(String[] args) {
SessionFactory sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
Session sessionToSave = sessionFactory.openSession();
Transaction txToSave = sessionToSave.beginTransaction();
txToSave.begin();
Map<String, String> extraProperties = new HashMap<String, String>(4);
extraProperties.put("pname1", "pvalue1");
extraProperties.put("pname2", "pvalue2");
extraProperties.put("pname3", "pvalue3");
extraProperties.put("pname4", "pvalue4");
Source source = new Source( new ExtraData("ename", extraProperties) );
sessionToSave.save(source);
txToSave.commit();
///////////////////////////////////////
Session sessionToFetch = sessionFactory.openSession();
Transaction txToFetch = sessionToFetch.beginTransaction();
txToFetch.begin();
Criteria criteria = sessionToSave.createCriteria(Source.class);
System.out.println( criteria.list().size() == 4 );
txToFetch.commit();
}
}
Thanks in advance.