object references an unsaved transient instance

I’ve faced with this error many times when trying to save an object to the db using hibernate.
The main reason is caused by the default behavior of hibernate to the relations between objects.
If you do not specify the cascade type in the hbm.xml file, then hibernate acts like it is a null relation which means
the related object is already in the db. For example, assume there are two concrete classes called Person and Address; Person has a one to many one directional
association with the Address..

public class Person {
private String name;
private String surname;
private Address address;



}

public class Address {
private String street;
private int zipcode;
private String city;



}

The association in the Person.hbm.xml will look like;

one-to-many name=”address” class=”Address”

Everything seems fine however when we try to save a person instance the error following exception will likely to occur.

//Assume you have the ready person instance at this point
Session session = sessions.openSession();
Transaction tx = session.beginTransaction();
session.save(Person);
tx.commit();
session.close();

object references an unsaved transient instance – save
the transient instance before flushing

The transient instance the error mentioned is the address of the person because hibernate thinks that the address is already in the db since you are not referring to a persistent address object.

In order to overcome this error, you can either define a cascade for the relation like “save-update, all etc”. or save the address before saving the person.

1) one-to-many name=”address” class=”Address” cascade=”all-delete-orphan”
After changing to this, the same code will work.

2) If you do not want to change the xml, you can try this;
//Assume you have the ready person instance at this point
Session session = sessions.openSession();
Transaction tx = session.beginTransaction();
session.save(person.getAddress());
session.save(person);
tx.commit();
session.close();

JSF Client Side Validators

JSF has no built-in support for “client side validations”, the goal of this component library is to bring this functionality to JSF. JSF Client Side Validation is implemented using a series of validator components.

There are several types of validators currently supported which are;

  1. Required Field Validator
  2. Range Validator
  3. Compare Validator
  4. Regular Expression Validator
  5. Length Validator
  6. Integer Only Validator
  7. Custom Validator
  8. Validation Summary

Common Attributes

  • componentToValidate : Server id of the component to be validated.
  • errorMessage : Text that is to be displayed when the input is invalid. (Can be bound to a bundle message.)
  • highlight : Changes the background color of the component with an invalid input.
  • display: Display mode of the error message.
    • none : Validation message is not displayed in the message location but displayed only in the validation summary.
    • static: Space for the message is allocated even the message is invisible.
    • dynamic: Space for the message is created dynamically if the message is visible.
  • style: Style class of the validation message


Validator Specific Attributes

  • Compare Validator
    • componentToCompare : Server id of the component whose value to be compared.
    • operator : “eq” or “not”. Controls whether the values are equal or not.
  • Required Field Validator
    • No more attributes other than the common ones.
  • Range Validator
    • minValue : Lower valid bound.
    • maxValue : Upper valid bound.
  • Regular Expression Validator
    • pattern:  Regular expression pattern to be checked.
  • Length Validator
    • min : Min length.
    • max: Max length
    • exactly : Exact length
  • Integer Only Validator
    • Comes as a javascript function that disables letters onKeyPress.
  • Custom Validator
    • Enables to plug custom validator functions to the library.
    • function: Function name
    • params: Parameters
  • Validation Summary
    • Shows the validation summary either in place or as a popup message box.

 

UPDATED: Version 0.9.1. containing several bug fixes is released. Checkout the changelog.

Detailed
Documentation and Download
* There
is a very detailed documentation at
jsf-comp.
* Both source and the distribution can be reached
at jsf-comp downloads section.
*
I’ve provided an example web application which can also be reached at
downloads section.
* In addition here is another entry of mine focused on the features. I suggest you to take a look at the features list and how this library differs from Shale and ADF client validation.
* Current version is 0.9.1 which contains bug fixes of version 0.9.0.

UPDATE: 07.02.2007
The project is no longer under development, please see the successor of this library which has joined Apache MyFaces.

Posted in Java. 35 Comments »

POI impressions (Combobox issue)

I’ve observed that POI is the most popular library to read/write excel files with java. Using poi I have implemented a class2Excel service that takes a class as an argument and generates an excel file by looking at the fields of the class. The idea is to generate an excel form for multiple entries of data. However I have this problem of excel comboboxes, on a jsp page, when trying to enter a new data, user selects from a combobox to enter the information about a field of the pojo for instance, the problem occurs here. I cannot port the entry mode of jsp form to the entry mode of an excel form. Because poi does not support to create a cell type of a combobox. The limitations faq at the poi home site states that pivot table thus comboboxes are not supported, so I’m stuck at this point. I hope the new version will support creating a cell type of a combobox.

Posted in General. Comments Off

could not initialize proxy – the owning session was closed

Hibernate proxies can be very handy if you have performance concerns, the idea is to load the objects lazily as proxies and never hit the database. The proxies are initialized meaning fetched from the database when they are first accessed, however life is not so easy:) Whether there is a configured filter-interceptor of spring to manage hibernate sessions or you manually take care of the session’s life cycle, once the session was closed, the proxies attached to that will lead to a proxy initialization error if they are accessed later.

The reason is tricky, a hibernate session is created and some data is loaded to the session as a proxy, later the session is closed without an access to the proxy occurred. Then proxy is decided to be used and accessed, at this point one may get the error “no session or session was closed” since the session which the proxy is attached is “closed”. In order to deal with this situation there are some solutions.

1) Reloading the proxy “before” using it in the new session, this will attach the new proxy to the new session. 

2) Using static method Hibernate.initialize(obj) before the owning session was closed, this will hit the db and replace the proxy with the actual data.

3) Using a Session lock via Session.lock(obj,lockmode), this will reattach the proxy to the new session.

As you see this proxy business isn’t simple as it seems but I belive once you learn how to use them they are the best way to optimize the performance.In the end who has not the need for speed?