Hibernate seq genration issue

I am trying to instantiate an entity that is mapped to database through hibernate. Id of this entity is generated through seq.

I have instantiated this entity inside thread and trying to save it. Each time seq
in multithreded environemnt, it creates same id.

It seems that hibernate sequence generator is not thread safe. Can anyone help me out in finding a way to make it thread safe in my multithreaded java app.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

rajeevy69Connect With a Mentor Author Commented:
Ajay-SinghConnect With a Mentor Commented:
Can you post the mapping file?
rajeevy69Author Commented:
mapping file looks like this:

    <class name="Employer" table="employers">
        <id name="id">
            <generator class="sequence">
                <param name="sequence">employer_id_seq</param>
        <property name="name"/>
     <property name="country"/>

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Ajay-SinghConnect With a Mentor Commented:
Your mapping looks OK, but the behavior is weired. Are you
getting the same sequence all the time?
rajeevy69Author Commented:
not always.

when accessing from different thread,this problem is happening. bu if i try from another instance it works fine.
Ajay-SinghConnect With a Mentor Commented:
Are you sharing the session across threads?
rajeevy69Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.