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 9059805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:03:56+00:00 2026-06-16T15:03:56+00:00

This seems to be a similar question ( Test DAO in java using Junit4

  • 0

This seems to be a similar question (Test DAO in java using Junit4 and Hibernate), but he’s getting the session created successfully. I’m unable to create the session.

Please note that I’m not using Spring. I just need to test my DAOs which create sessions using HibernateUtil.java

My Sample Test Code (in test packages)

public void testGetBean() {
    System.out.println("getBean");
    int idaccount = 0;
    Account expResult = null;
    Account result = DAOAccount.getBean(idaccount);
    assertEquals(expResult, result);
    // TODO review the generated test code and remove the default call to fail.
    //fail("The test case is a prototype.");
}

And here is DAOAccount getBean() (in source packages)

public static Account getBean(int idaccount) {
    Account ac = null;
    Session sess = NewHibernateUtil.getSessionFactory().openSession();
    Transaction tx = sess.beginTransaction();
    Query q = sess.createQuery("from Account where idaccount=:id");
    q.setInteger("id", idaccount);
    ac = (Account) q.uniqueResult();
    tx.commit();
    sess.close();
    return ac;
}

NewHibernateUtil.java (in source packages)

public class NewHibernateUtil {
private static final SessionFactory sessionFactory;

static {
    try {
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
//            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        sessionFactory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
}

and here is the stack trace enter image description here

Below is the project structure :

enter image description here

Actual Exception Stacktrace as asked by @Subin

[2012-12-29 16:36:24.347] [ INFO] Configuration.applyHibernateValidatorLegacyConstraintsOnDDL:1646 - Hibernate Validator not found: ignoring
Initial SessionFactory creation failed.java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/validation/Validation
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/validation/Validation
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:170)
    at org.hibernate.cfg.beanvalidation.BeanValidationActivator.applyDDL(BeanValidationActivator.java:103)
    at org.hibernate.cfg.Configuration.applyBeanValidationConstraintsOnDDL(Configuration.java:1674)
    at org.hibernate.cfg.Configuration.applyConstraintsToDDL(Configuration.java:1624)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1415)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1826)
    at dao.NewHibernateUtil.<clinit>(NewHibernateUtil.java:25)
    at dao.DAOAccount.getAccounts(DAOAccount.java:77)
    at dao.DAOAccountTest.testGetAccounts_User(DAOAccountTest.java:78)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
  • 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-06-16T15:03:57+00:00Added an answer on June 16, 2026 at 3:03 pm

    public Configuration configure()
    throws HibernateException
    Use the mappings and properties specified in an application resource named hibernate.cfg.xml.
    Throws: HibernateException

    Configuration.configure() will work correctly only if hibernate.cfg.xml is available as application resource. or classpath.

    If you are using eclipse to invoke your Junit test, add hibernate.cfg.xml to classpath, since you are using maven do you have it in src/main/resources

    Please see this link to see, why java.lang.ClassFormatError: happens.
    Maven: hibernate-entitymanager together with javaee-api break my unit tests

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There seems to be a similar question to this on here but with the
I've seen a bunch of similar questions to this, but nothing seems to be
Sorry if this is a dupe. There are many similar questions but it seems
Note: I've asked this question in a similiar format on superuser but it seems
This seems a 'stupid' question, but let me explain. I'm working for a company
This seems like a repeated question but i'm not able to get my answer.
This seems to be primary Question. but i search it and i couldn't find
I'm trying to do something similar to this question , but it's a bit
I realise this question is similar to this one but I think mine is
This seems similar to a number of other questions posted, but is actually a

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.