Thanks for the answer.
If I understand, what you're saying is that if I want hibernate to be able to do a binding, I need to refer to the whole object?
I was trying to do a binging that way because in fact I have a bigger problem I am trying to solve, which is a performance problem, and no mather how I try, I always get something which does not work.
To give you a more concrete exemple, I have a table DataFile containing info about files. Those files contain mathematical data for pattern recognition. Each file contains around 300 data which are read from the file and written in table Fp . So for each entry in table DataFile, there will be around 300 in table Fp.
Table Fp is very very big, and we want it to be as fast as possible, and not need any join in queries. Because performance is critical, we've been testing using MySQL with a MYIsam DB with no foreign key enforced nowhere. Only indexes. And because the data manipulation for writing is very straightforward and simple, the database (and Hibernate) is not transactional neither.
And the problem I have been having is that when I try to insert the content of a file, I get 300 insert on table Fp.
The first code I did was this: (We are using Hibernate with Spring)
Code:
public void insertData(List<Long> datas, Long fId) {
HibernateTemplate hibTemplate = this.getHibernateTemplate();
for(Long data: datas) {
Fp fp = new Fp(data, fId);
hibTemplate.persist(fp);
}
}
I knew, of course, with this I would not get bulk insert, but I wanted a working code first.
I though that by putting a collection of Fp in the class DataFile, filling the content of the collection and calling persist on class DataFile, Hibernate would do batch insert for the Collection. But to be able to set the fId in the class Fp, I need first to persist DataFile without the collection of Fp datas to get the value for fId. The code looks like this:
Code:
DataFile df = new DataFile();
// ... sets other attributes
getHibernateTemplate().persist(df);
List<Fp> fps = getFpList(file, df.getId()); // gets the list of data from file
df.getFps().addAll(fps);
getHibernateTemplate.saveOrUpdate(df); // I've tried with persist too.
But this does not work. I get the following error message:
java.sql.BatchUpdateException: failed batch.
Another way I've tried is to map a collectionOfElements, and not use a Fp class at all. There is no other attribute in the Fp class so this is possible. The class DataFile contains the following annotation:
Code:
@CollectionOfElements
@JoinTable(name="Fp", joinColumns = @JoinColumn(name="fId"))
@Column(name="data", nullable=false)
public List<long> getFpData()
If I completly remove the class Fp (so no entity class is declared on table Fp), This does in fact works, even if it creates a table Fp without a pk (I've let Hibernate generate the schema to see). I can create all the data, do one persist and I presume Hibernate is able to do bulk inserts.
But it does break a custom query on the Fp table, which I have not been able to modify to work with this way of mapping. The query looks like this:
Code:
@NamedQuery(name="searchData",
query="select new com.xyz.SearchData(fp.fId, count(fp) from Fp fp where fp.fId in (:fpList) group by fp.fId order by count(fp) desc)
How do I write this query if I don't have an entity class Fp?
Thanks for your help,
Llaurick