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.
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();