Hi all,
i had tried a program to persist in a table by passing the enitity-name in
session.save("Customer1", customer), which is defined in hibernate mapping file. I have single entity class and it mapped to more than one table
(Each table has the same schema structure) using the entity-name property in the class tag. According to the entity name the data will store in the appropriate tables. Its works well while using the session factory to create a session.
Then i have tried the same concept using hibernate with jpa and used EntityManagerFactory to create session. i have faced the hibernate mapping exception.
Coding for mapping file Customer.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.dms.sample.Customer" table="Customer1" entity-name="Customer1">
<id name="CustomerId" column="CUSTOMER_ID" type="java.lang.Integer">
<generator class="identity" />
</id>
<property name="Name" column="CUSTOMER_NAME" type="string"/>
<property name="Age" column="CUSTOMER_AGE" type="java.lang.Integer"/>
</class>
<class name="com.dms.sample.Customer" table="Customer2" entity-name="Customer2">
<id name="CustomerId" column="CUSTOMER_ID" type="java.lang.Integer">
<generator class="identity" />
</id>
<property name="Name" column="CUSTOMER_NAME" type="string"/>
<property name="Age" column="CUSTOMER_AGE" type="java.lang.Integer"/>
</class>
</hibernate-mapping>
Coding for hibernate.cfg.xml
Code:
<?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 name="">
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sampe_test_db</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="META-INF/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Coding for persistence.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="hibernateProofDS" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml"/>
</properties>
</persistence-unit>
</persistence>
Exception:
Code:
12:41:52,151 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-4) MSC00001: Failed to start service jboss.persistenceunit."HiberanteConfiguration-0.0.1-SNAPSHOT.war#hibernateProofDS": org.jboss.msc.service.StartException in service jboss.persistenceunit."HiberanteConfiguration-0.0.1-SNAPSHOT.war#hibernateProofDS": Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1767) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885) [rt.jar:1.6.0_06]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907) [rt.jar:1.6.0_06]
at java.lang.Thread.run(Thread.java:619) [rt.jar:1.6.0_06]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: hibernateProofDS] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
... 3 more
Caused by: org.hibernate.MappingException: Unknown entity: com.dms.sample.Customer
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1031)
at org.hibernate.ejb.metamodel.AttributeFactory.getDeclarerEntityMetamodel(AttributeFactory.java:221)
at org.hibernate.ejb.metamodel.AttributeFactory.access$600(AttributeFactory.java:62)
at org.hibernate.ejb.metamodel.AttributeFactory$4.resolveMember(AttributeFactory.java:910)
at org.hibernate.ejb.metamodel.AttributeFactory.determineAttributeMetadata(AttributeFactory.java:423)
at org.hibernate.ejb.metamodel.AttributeFactory.buildAttribute(AttributeFactory.java:91)
at org.hibernate.ejb.metamodel.MetadataContext.wrapUp(MetadataContext.java:185)
at org.hibernate.ejb.metamodel.MetamodelImpl.buildMetamodel(MetamodelImpl.java:64)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:91)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
Have any one faced these issuse? Anyone help me to get out of this error..
Thanks in advance..!