1

Hello I am new in hibernate and at the moment I'm trying to do something in practice. But have some errors.

I try to use hibernate in my project. I read the tutorial and start to integrate Hibernate into my java maven project. So first of all I added the next dependencies:

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.0.6.Final</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>

The next step was to add hibernate.cfg.xml file into the src/main/resources directory. This file looks like:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

    <property name="hibernate.connection.url">jdbc:mysql://localhost/testprojectdatabase</property>
    <property name="hibernate.connection.username">username_here</property>
    <property name="hibernate.connection.password">password_here</property>

    <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50</property>

    <mapping resource="milkiv/mytestproject/models/UserInfo.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Then I create UserInfo.java for mapping user_info table and put it inside src/main/java/milkiv/mytestproject/models/UserInfo.java, and UserInfo.hbm.xml file which I put inside src/main/resources/milkiv/mytestproject/models/UserInfo.hbm.xml.

I have also created the class UserInfoManager which look like:

public class UserInfoManager implements ManagerAdd<UserInfo> {
    private final SessionFactory factory;

    public UserInfoManager() {
    factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
    }

    public int add(UserInfo user) {
    Transaction transaction = null;
    Integer userId = null;
    try (Session session = factory.openSession()){
        transaction = session.beginTransaction();
        userId = (Integer) session.save(user);
        transaction.commit();
    } catch (HibernateException he) {
        if (transaction != null) {
        transaction.rollback();
        }
    }
    return userId;
    }
}

When I try to create test for method add(UserInfo user) it failed on UserInfoManager constructor on factory initialization because of

Mapping (RESOURCE) not found : milkiv/mytestproject/models/UserInfo.hbm.xml : origin(milkiv/mytestproject/models/UserInfo.hbm.xml)

So eventually I know where, but do not know how to fix this. I have read a lot of answers for this questions on stackoverflow but no one didn't help me to fix this error.

I will appreciate any ideas, help and explanation how to fix this problem...

full stack trace: Mapping (RESOURCE) not found : milkiv/mytestproject/models/UserInfo.hbm.xml : origin(milkiv/mytestproject/models/UserInfo.hbm.xml) org.hibernate.boot.MappingNotFoundException at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:56) at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:274) at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:413) at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724) at milkiv.mytestproject.managers.UserInfoManager.(UserInfoManager.java:22) at milkiv.mytestproject.managers.UserInfoManagerTest.testAdd(UserInfoManagerTest.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)

UPDATE If anyone need, solving is in the comments for @v.ladynev answer.

Nickolas
  • 192
  • 7
  • 20

2 Answers2

3

I check your approach and everything works fine.

When you put UserInfo.hbm.xml in the src/main/resources/milkiv/mytestproject/models/UserInfo.hbm.xml

it will appear in the

bin/milkiv/mytestproject/models/UserInfo.hbm.xml

after build (instead of bin can be your build folder).

So /milkiv/mytestproject/models/ looks like as other source packages in the build folder. Please, check it. Check that you have resource folder in the classpath.

Try to add / in the beginning

<mapping resource="/milkiv/mytestproject/models/UserInfo.hbm.xml"/>

Update

Adding / in the beginning doesn't matter, because of Hibernate remove it before loading by ClassLoader. So the most valid way — without / in the beginning.

Try to put UserInfo.hbm.xml in the root of resources

<mapping resource="UserInfo.hbm.xml"/>

Update

Try to test

 public UserInfoManager() {
        System.out.println(UserInfoManager.class
                .getResource("/milkiv/mytestproject/models/UserInfo.hbm.xml"));

        System.out.println(ClassLoader.getSystemClassLoader().getResource(
                "milkiv/mytestproject/models/UserInfo.hbm.xml"));
}

Note leading / in the first case. What console output after

new UserInfoManager();
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • @NickolasUA Did you check `UserInfo.hbm.xml` in the build folder? – v.ladynev Mar 02 '16 at 07:30
  • It is maven project. So eventually it appear in target\classes\resources\milkiv\mytestproject\models – Nickolas Mar 02 '16 at 07:43
  • @NickolasUA After build jar or war where `/milkiv/mytestproject/models/UserInfo.hbm.xml` resides? It should be in the root of compiled sources folder (`classes`) without any `resources`. Try to specify `resources/milkiv/mytestproject/models/UserInfo.hbm.xml`. – v.ladynev Mar 02 '16 at 07:58
  • it is null in both cases. What can it mean? – Nickolas Mar 02 '16 at 08:02
  • @NickolasUA Hibernate loads `hbm.xml` using `ClassLoader` — the second line. Try the same with `resources\milkiv\mytestproject\models\UserInfo.hbm.xml`. When you get a valid path, Hibernate will work fine too. – v.ladynev Mar 02 '16 at 08:20
  • it is locating actually inside resources folder. The full path is: target\classes\resources\milkiv\mytestproject\models\UserInfo.hbm.xml. – Nickolas Mar 02 '16 at 08:25
  • Thank's a lot for you patience and help. It work now. – Nickolas Mar 02 '16 at 08:35
  • @NickolasUA You are welcome. What have you done for it? – v.ladynev Mar 02 '16 at 08:42
  • I specified the ways to *.hbm.xml files with the "resources" folder, as you said... – Nickolas Mar 02 '16 at 19:00
  • @NickolasUA It is strange that Maven creates the `resources` folder in the `classes`. – v.ladynev Mar 02 '16 at 20:49
  • I don't know all the details of working maven because I am new on it. But can it depends from the plugin which I use to build and deploy project? I use the next plugin with next configuration: org.apache.tomcat.maven tomcat7-maven-plugin /${project.name} tomcat – Nickolas Mar 02 '16 at 21:57
  • @NickolasUA I know nothing about this plugin, sorry. – v.ladynev Mar 02 '16 at 22:24
1

@NickolasUA you are using Hibernate 5. So change the last line in hibernate.cfg.xml

<mapping class="milkiv.mytestproject.models.UserInfo.hbm.xml"/>
Pon Akilan
  • 11
  • 1