Hibernate Session.save () does not return a value?

Below code throws casting error

Long newID = (long) session.save (object);

I am new to sleep mode. I do not know why.

+6
java hibernate
source share
2 answers

The return value of session.save() depends on your mapping. Most likely you have an ID type that is not long. Try to do this:

 System.out.println(session.save(object).getClass().getName()); 

Then you will see the type name.

+10
source share

There are two methods:

  • public Serializable save(Object object) throws HibernateException
    Hold this transient instance by assigning the generated identifier first. (Or using the current value of the identifier property if an assigned generator is used.) This operation cascades to related instances if the association is mapped to cascade="save-update" .
    Parameters: - a transition instance of a constant class
    Returns: generated identifier

  • public Serializable save(String entityName, Object object) throws HibernateException
    Hold this transient instance by assigning the generated identifier first. (Or using the current value of the identifier property if an assigned generator is used.) This operation cascades to related instances if the association is mapped to cascade="save-update" .
    Parameters: - a transition instance of a constant class
    Returns: generated identifier

+2
source share

All Articles