Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 734563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:23:15+00:00 2026-05-14T07:23:15+00:00

I’m trying to test Hibernate mappings, specifically a unique constraint. My POJO is mapped

  • 0

I’m trying to test Hibernate mappings, specifically a unique constraint. My POJO is mapped as follows:

<property name="name" type="string" unique="true" not-null="true" />

What I want to do is to test that I can’t persist two entities with the same name:

public class ExpertiseAreaDAOTest{
    private ExpertiseAreaDAO ead;
    static Connection c;
    private static SessionFactory sessionFactory;

    static {
        Configuration config = new Configuration().configure("resources/exp/hibernate-test.cfg.xml");
        sessionFactory = config.buildSessionFactory();
    }

    @Test(expected=ConstraintViolationException.class)
    public void testPersistTwoExpertiseAreasWithTheSameNameIsNotAllowed(){
        ExpertiseArea ea = new ExpertiseArea("Design");
        ExpertiseArea otherEA = new ExpertiseArea("Design");

        ead.setSession(getSessionFactory().getCurrentSession());
        ead.getSession().beginTransaction();
        ead.makePersistent(ea);
        ead.makePersistent(otherEA);
        ead.getSession().getTransaction().commit(); 
    }

    public static SessionFactory getSessionFactory(){
        return sessionFactory;
    }
}
//Other methods ommited

On commiting the current transaction, I can see in the logs that a ConstraintViolationException is thrown:

16:08:47,571 DEBUG SQL:111 - insert into ExpertiseArea (VERSION, name, id) values (?, ?, ?)
Hibernate: insert into ExpertiseArea (VERSION, name, id) values (?, ?, ?)
16:08:47,571 DEBUG SQL:111 - insert into ExpertiseArea (VERSION, name, id) values (?, ?, ?)
Hibernate: insert into ExpertiseArea (VERSION, name, id) values (?, ?, ?)
16:08:47,572  WARN JDBCExceptionReporter:100 - SQL Error: -104, SQLState: 23505
16:08:47,572 ERROR JDBCExceptionReporter:101 - integrity constraint violation: unique constraint or index violation; SYS_CT_10036 table: EXPERTISEAREA
16:08:47,573 ERROR AbstractFlushingEventListener:324 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

So I would expect the test to pass, since the expected ConstraintViolationException is thrown. However, the test never completes (neither pass nor fails) and I have to manually kill the test runner.

What’s the correct way to test this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:23:16+00:00Added an answer on May 14, 2026 at 7:23 am

    Shouldn’t you run the creates in two transactions, expecting the first one to succeed and the second to fail?

    public void testPersistTwoExpertiseAreasWithTheSameNameIsNotAllowed(){
        ExpertiseArea ea = new ExpertiseArea("Design");
        ExpertiseArea otherEA = new ExpertiseArea("Design");
    
        SessionFactory sf = getSessionFactory();
        Session session = sf.openSession();
        Transaction txn = session.beginTransaction();
        try {
            ead.setSession(session);
            ead.makePersistent(ea);
            // no ExpertiseArea by this name in DB, this should commit
            txn.commit(); 
        } catch (HibernateException ex) {
            txn.rollback();
            fail("Unexpected exception " + ex);
        } finally {
            session.close();
            ead.setSession(null);
        }
        session = sf.openSession();
        txn = session.beginTransaction();
        try {
            ead.setSession(session);
            ead.makePersistent(otherEA);
            // this should fail 'cos there is already an ExpertiseArea with
            // the same name in DB
            txn.commit();
            fail("Expected constraint violation exception");
        } finally {
            session.close();
            ead.setSession(null);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.