简体   繁体   中英

java - hibernate save and read from DB

I am new in Java and use hibernate.

I added in my code datapoints instances:

Datapoint dp = new Datapoint();
dp.setDataset(dataset);
dp.setStation(station);

I run the dataset.getDatapointCount() function which has to count the dp with the dataset id:

public int getDatapointCount()
    {

        Criteria        crit = Database.getSession().createCriteria(Datapoint.class);

        crit = crit.add(Restrictions.eq("dataset", this));
        crit.setProjection(Projections.rowCount());

        Integer         result = (Integer)crit.uniqueResult();

        return result.intValue();
    }

and I got 0 dataPoints. (when the run is finished I see the new datapoints in the database).

I added

 Database.getSession().flush(); 

before the getDatapointCount() function and realy return the new number (1000 for example)

but in the end of the runing the datapoints didnt saved in the database!

In addition, if I write the getDatapointCount() after the filush() twice ,I got in the first

time the right answer(1000) and in the second time no right answer (0)

Can some one help me?

Thanks in advance.

What value of FlushMode are you using ?

A transaction not commited can be the reason that your data are not saved into DB.

See Hibernate FlushMode doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM