I am migrating entity mapping from an annotations app to hbm.xml
I error this error and not found the solution. Thanks
java.lang.Exception: Not an entity: class com.enterprise.package.user.domain.User
I have hibernate.cfg.xml in the resources folder, User.hbm.xml in one package and User.class in another
User.hbm.xml
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.enterprise.package.role.domain.User" table="user">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<property name="username" column="username" type="java.lang.String" unique="true"/>
<property name="password" column="password" type="java.lang.String"/>
<many-to-one name="role" column="role_id" class="com.enterprise.package.role.domain.Role" not-null="true"/>
</class>
</hibernate-mapping>
User.class
public final class User {
private Long id;
private String username;
private String password;
private Role role;
public User() {
}
public User(Long id) {
this.id = id;
}
public User(Long id, String username, String password, Role role) {
this.id = id;
this.username = username;
this.password = password;
this.role = role;
}
...
}
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">**</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="show_sql">true</property>
<mapping package="com/enterprise/package/user/infrastructure/persistence/hibernate/Role.hbm.xml"/>
<mapping package="com/enterprise/package/user/infrastructure/persistence/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I think the package of User class may be wrong in User.hbm.xml (we cant see in your snippet the package of this class)...
In User.hbm.xml instead of
com.enterprise.package.role.domain.User
may be
com.enterprise.package.user.domain.User.
Related
EDIT :
The problem solved,I didnt write the exactly loaction of the entites in the XML files,Where the one-many-tag.
Im trying to map these classes to database and i get this error :
Exception in thread "main" org.hibernate.MappingException: Association references unmapped class: Reservation
at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2557)
at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2808)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at entities.chairandtabletest.main(chairandtabletest.java:39)
I tried to find where is my problem,I read about this exception and i dont understand where is the problem in my program.
The classes :
public class Customer {
private Integer id;
private String fullName;
private String address;
private String email;
private String phoneNumber;
private List<Reservation> reservations = new ArrayList<Reservation>();
}
public class Reservation{
private Integer id;
private String date;
private Integer totalSum;
private boolean isDeliever;
private List<Item> items = new ArrayList<Item>();
}
public class Item {
private Integer id;
private String name;
private Integer price;
}
The tables :
CREATE TABLE ITEM(
item_id INT NOT NULL AUTO_INCREMENT,
item_name VARCHAR(40),
item_price INT NOT NULL,
reservation_id INT,
PRIMARY KEY(item_id)
);
CREATE TABLE RESERVATION(
reservation_id INT NOT NULL AUTO_INCREMENT,
reservarion_date VARCHAR(255),
reservtion_delivery BOOL,
reservation_total_sum INT,
customer_id INT,
PRIMARY KEY(reservation_id)
);
CREATE TABLE CUSTOMERS(
customer_id INT NOT NULL AUTO_INCREMENT,
customer_full_name VARCHAR(40),
customer_address VARCHAR(255),
customer_email VARCHAR(255),
customer_phone_number VARCHAR(20),
PRIMARY KEY(customer_id)
);
The XML files :
<?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://127.0.0.1:3306/chairandtable?serverTimezone=UTC</property>
<property name="hibernate.connection.username">aliali</property>
<property name="hibernate.connection.password">password</property>
<mapping resource="props/customer.hbm.xml"/>
<mapping resource="props/reservation.hbm.xml"/>
<mapping resource="props/item.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entities.Customer" table="customers">
<meta attribute="class-description">
This class contain customers details.
</meta>
<id name="id" type="integer" column="customer_id">
<generator class="identity"/>
</id>
<bag name="reservations" cascade="all">
<key column= "customer_id"/>
<one-to-many class="Reservation"/>
</bag>
<property name="fullName" column="customer_full_name" type="string"/>
<property name="address" column="customer_adress" type="string"/>
<property name="email" column="customer_email" type="string"/>
<property name="phoneNumber" column="customer_phone_number" type="string"/>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name ="entities.Reservation" table="reservation">
<meta attribute="class-description">
This class contain reservation detail.
</meta>
<id name ="id" column="reservation_id" type="integer">
<generator class="identity"/>
</id>
<bag name="items" cascade="all">
<key column="reservation_id"/>
<one-to-many class="Item"/>
</bag>
<property name="date" column="reservation_date" type="string"/>
<property name="totalSum" column="reservation_total_sum" type="integer"/>
<property name="isDeliver" column="reservtion_delivery" type="boolean"/>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entities.Item" table="item">
<id name= "id" column="item_id" type="integer">
<generator class="identity"/>
</id>
<property name="name" column="item_name" type="string"/>
<property name="price" column="item_price" type="integer"/>
</class>
</hibernate-mapping>
I think the problem is the tables i declared , Maybe the are not matching the
xml file.
You need to change the order of *hbm.xml configs to:
<mapping resource="props/item.hbm.xml"/>
<mapping resource="props/reservation.hbm.xml"/>
<mapping resource="props/customer.hbm.xml"/>
And add the "entities" package to all of your one-to-many mappings like this:
<one-to-many class="entities.Item"/>
I am encountering an error when I implement Hibernate JPA mapping when I built it in Apache Maven.
Description Resource Path Location Type
The persistence.xml file does not have supported content for this JPA
platform. persistence.xml /LearningManagementService/src/main/resources/META-INF JPA Problem
My src/main/java has a package of com.ph.deped.model
User.java
package com.ph.deped.model;
/**
* User generated by hbm2java
*/
public class User implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = -2357935130826100477L;
private long id;
private Privilege privilege;
private String username;
private String password;
public User() {
}
public User(Privilege privilege, String username, String password) {
this.privilege = privilege;
this.username = username;
this.password = password;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public Privilege getPrivilege() {
return this.privilege;
}
public void setPrivilege(Privilege privilege) {
this.privilege = privilege;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
Privilege.java
package com.ph.deped.model;
// Generated May 7, 2018 9:04:03 PM by Hibernate Tools 5.2.3.Final
import java.util.HashSet;
import java.util.Set;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Privilege generated by hbm2java
*/
public class Privilege implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 910980850904284147L;
private long id;
private String description;
#JsonIgnore
private Set<User> users = new HashSet<User>(0);
public Privilege() {
}
public Privilege(String description, Set<User> users) {
this.description = description;
this.users = users;
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public Set<User> getUsers() {
return this.users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
then my hbm.xml mapping is located in src/main/resources with a package of com.ph.deped.model same with the entity objects
User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-access="property" default-
cascade="none" default-lazy="true">
<class catalog="elearning" dynamic-insert="false" dynamic-update="false" mutable="true" name="com.ph.deped.model.User" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="user">
<id name="id" type="long">
<column name="id"/>
<generator class="identity"/>
</id>
<many-to-one class="Privilege" embed-xml="true" fetch="select" insert="true" name="privilege" not-found="exception" optimistic-lock="true" unique="false" update="true">
<column name="privilege_id"/>
</many-to-one>
<property generated="never" lazy="false" name="username" optimistic-lock="true" type="string" unique="false">
<column name="username"/>
</property>
<property generated="never" lazy="false" name="password" optimistic-lock="true" type="string" unique="false">
<column name="password"/>
</property>
</class>
</hibernate-mapping>
Privilege.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true" default-access="property" default-
cascade="none" default-lazy="true">
<class catalog="elearning" dynamic-insert="false" dynamic-update="false" mutable="true" name="com.ph.deped.model.Privilege" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="privilege">
<id name="id" type="long">
<column name="id"/>
<generator class="identity"/>
</id>
<property generated="never" lazy="false" name="description" optimistic-lock="true" type="string" unique="false">
<column name="description"/>
</property>
<set embed-xml="true" fetch="select" inverse="true" lazy="true" mutable="true" name="users" optimistic-lock="true" sort="unsorted" table="user">
<key on-delete="noaction">
<column name="privilege_id"/>
</key>
<one-to-many class="User" embed-xml="true" not-found="exception"/>
</set>
</class>
</hibernate-mapping>
and lastly my persistence.xml is located at META-INF/persistence.xml under src/main/resources
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_1_1.xsd"
version="1.1">
<persistence-unit name="Elearning" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<mapping-file>/com/ph/deped/model/UserLogin.hbm.xml</mapping-file>
<mapping-file>/com/ph/deped/model/Privilege.hbm.xml</mapping-file>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="1a0b1c6d0d6c1b3aA#"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.enable_lazy_load_no_trans" value="true"/>
<property name="hibernate.show_sql" value="false"/>
</properties>
</persistence-unit>
</persistence>
I am getting ClassCastException: at.sheldor5.tr.api.time.RecordType cannot be cast to java.lang.Boolean because Hibernate 5.2.9.FINAL completly ignores my converter:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="at.sheldor5.tr.api.time">
<class name="Record" table="RECORDS">
<id name="id" type="int" column="PK_RECORD_ID">
<generator class="native"/>
</id>
<many-to-one name="user" column="FK_USER_MAPPING_ID" class="at.sheldor5.tr.api.user.UserMapping"/>
<property name="date" type="java.time.LocalDate" column="DATE" index="I_DATE"/>
<property name="time" type="java.time.LocalTime" column="TIME"/>
<property name="type" type="boolean" column="TYPE">
<type name="at.sheldor5.tr.persistence.converter.RecordTypeAttributeConverter"/>
</property>
</class>
</hibernate-mapping>
Converter:
#Converter
public class RecordTypeAttributeConverter implements AttributeConverter<RecordType, Boolean> {
#Override
public Boolean convertToDatabaseColumn(final RecordType attribute) {
if (attribute == null) {
return null;
}
return attribute.getBoolean();
}
#Override
public RecordType convertToEntityAttribute(final Boolean dbData) {
return RecordType.getType(dbData);
}
}
The only thing I can see is that hibernate ignores my converter because of some random things.
Any suggestions (except clean/rebuild which I already tried or hard reset)?
EDIT: my converter won't even gets loaded ...
I am using Hibernate project in my eclipse with oracle database and java application.
after added new class(exist in my database) to my Hibernate project, and run my java project.it's loaded successfully also allow to add data from java project to hibernate table but at add these data to database i got this error
Unknown entity: com.se.automation.db.client.mapping.DocumentWRONGMODDATESTATUS
here is my code to add data to my database.
note ( I opened the session and i test it with another class exist in the same Hibernate project with the same structure to this class "Hibernate Project" )
DocumentWRONGMODDATESTATUS docstatus = new DocumentWRONGMODDATESTATUS();
docstatus.setOriginalDocId(doc.getPdf().getId());
at this moment every thing fine , but below line give the error(test it in debugging mode)
addDocumentWrongModDateStatus(docstatus ,Session );
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Enable Hibernate's automatic session context management -->
<property name="hibernate.connection.pool_size">1</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="connection.autoReconnect">true</property>
<!-- <property name="show_sql">true</property> -->
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="connection.autoReconnectForPools">true</property>
<property name="connection.is-connection-validation-required">true</property>
<!-- auto commit -->
<property name="connection.autocommit">true</property>
<!-- configuration pool via c3p0 -->
<property name="c3p0.idleConnectionTestPeriod">1000</property>
<property name="c3p0.initialPoolSize">1</property>
<property name="c3p0.maxPoolSize">1</property>
<property name="c3p0.maxIdleTime">1</property>
<property name="c3p0.maxStatements">3</property>
<property name="c3p0.minPoolSize">1</property>
<mapping resource="com/se/automation/db/client/mapping/DocumentWRONGMODDATESTATUS.hbm.xml" />
<mapping
resource="com/se/automation/db/client/mapping/DocumentCompareChangedFet.hbm.xml" />
<mapping
resource="com/se/automation/db/client/mapping/DocumentCompareCode.hbm.xml" />
<mapping
resource="com/se/automation/db/client/mapping/DocumentCompareComment.hbm.xml" />
<mapping
resource="com/se/automation/db/client/mapping/DocumentCompareMethod.hbm.xml" />
</session-factory>
</hibernate-configuration>
DocumentWRONGMODDATESTATUS.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.se.automation.db.client.mapping.DocumentWRONGMODDATESTATUS"
table="DOCUMENTWRONGMODDATESTATUS">
<id column="ID" length="33" name="id" type="long">
<generator class="assigned" />
</id>
<property name="Status" type="java.lang.String">
<column name="STATUS" />
</property>
<property name="OriginalDocId" type="java.lang.Long">
<column name="ORIGINALDOCID" />
</property>
<property name="RevDocId" type="java.lang.Long">
<column name="REVDOCID" />
</property>
</class>
</hibernate-mapping>
DocumentWRONGMODDATESTATUS.java
package com.se.automation.db.client.mapping;
import java.io.Serializable;
public class DocumentWRONGMODDATESTATUS implements Serializable
{
private static final long serialVersionUID = 1L;
private Long id;
private String Status;
private Long OriginalDocId;
private Long RevDocId;
public Long getOriginalDocId()
{
return OriginalDocId;
}
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getStatus()
{
return Status;
}
public void setStatus(String status)
{
Status = status;
}
public void setOriginalDocId(Long originalDocId)
{
OriginalDocId = originalDocId;
}
public Long getRevDocId()
{
return RevDocId;
}
public void setRevDocId(Long revDocId)
{
RevDocId = revDocId;
}
{
}
}
When I tried my first hibernate program and I got this error.
My program simply insert empID,first_name,second_name(table names) and using MySQL database.
Tried lot of solutions sill getting this error.
mapping file
<?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.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">anandhu</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update </property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Configuration file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Employee" table="emp">
<id name="empID" column="empID" type="int">
<generator class="assigned"></generator>
</id>
<property name="firstName" column="first_name" type="string">
<property name="lastName" column="second_name" type="string">
</class>
</hibernate-mapping>
This is my persistence class
public class Employee {
private int empID;
private String firstName;
private String lastName;
public int getID(){
return empID;
}
public void setID(int empID){
this.empID = empID;
}
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
}
This is my test class
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class EmployeeTest {
public static void main(String[] args) {
Configuration cf = new Configuration();
cf.configure("employee.cfg.xml");
SessionFactory factory = cf.buildSessionFactory();
Session session = factory.openSession();
Employee employee = new Employee();
employee.setID(30);
employee.setFirstName("raju");
employee.setLastName("ryan");
Transaction transaction = session.beginTransaction();
session.save(employee);
System.out.println("updated");
transaction.commit();
session.close();
factory.close();
}
}
stack trace
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: employee.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at com.anandhu.hibernateproject.EmployeeTest.main(EmployeeTest.java:16)
Caused by: org.dom4j.DocumentException: Error on line 3 of document :
The processing instruction target matching "[xX][mM][lL]" is not allowed.
Nested exception:
The processing instruction target matching "[xX][mM][lL]" is not allowed.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
... 2 more
The problem with this lines
<property name="firstName" column="first_name" type="string">
<property name="lastName" column="second_name" type="string">
Should be
<property name="firstName" column="first_name" type="string" />
<property name="lastName" column="second_name" type="string" />
It would be more simply to find a solution if you show all stacktrace.
You can find examples of working with a configuration and a session here.
Upated
I check this mapping and it works fine. Looks like you have incorrect symbols (may be spaces) in the xml header. May be this will be helpfull Error: The processing instruction target matching “[xX][mM][lL]” is not allowed.