I switched the mapping in my code from xml resource to annotations and got that exception. I don't see the error. I think you can figure it out from my code:
hibernate.cfg.xml
<?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.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">####</property>
<property name="hibernate.connection.username">##</property>
<property name="hibernate.connection.password">###</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="hibernate.show_sql">true</property>
<mapping class="max.Trade" file="" jar="" package="max" resource=""/>
</session-factory>
</hibernate-configuration>
Trade.java
#Entity
#Table(name="TRADES", schema="PGT")
public class Trade implements java.io.Serializable
{
private long murexId;
private String type;
private String portfolio;
public Trade() {
}
public Trade(long murexId) {
this.murexId = murexId;
}
public Trade(long murexId, String type, String portfolio) {
this.murexId = murexId;
this.type = type;
this.portfolio = portfolio; }
#Id
#Column(name="MUREX_ID", unique=true, nullable=false, precision=10, scale=0)
public long getMurexId() {
return this.murexId;
}
public void setMurexId(long murexId) {
this.murexId = murexId;
}
#Column(name="TYPE", length=32)
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
#Column(name="PORTFOLIO", length=32)
public String getPortfolio() {
return this.portfolio;
}
public void setPortfolio(String portfolio) {
this.portfolio = portfolio;
}
}
I will really appreciate any help.
Thanks!
replace mapping definition(tag) as
<mapping class="max.Trade"/>
Related
I'm trying to fetch resultset by making the query in Hibernate like
String id = "10";
Person per = session.load(Person .class, id); // This is wrong, because it accepts only an integer, not a string.
But, I require to fetch results using session.load by passing a string, because second level cache is not triggering when I try to fetch as
Criteria cr = session.createCriteria(Person.class);
cr.add(Restrictions.like("userId", id));
List<?> results = cr.list();
Person class
#Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
#Entity
#Table(name = "person")
public class Person {
#Override
public String toString() {
return "Personalisation [id=" + id + ", userId=" + userId
+ ", courseId=" + courseId + ", courseValue=" + courseValue
+ "]";
}
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String userId;
private String courseId;
private String courseValue;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
public String getCourseValue() {
return courseValue;
}
public void setCourseValue(String courseValue) {
this.courseValue = courseValue;
}
}
<?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.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/intu</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Ehcache config -->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property>
<!-- c3p0 Connection pool config -->
<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">100</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.c3p0.privilegeSpawnedThreads">true</property>
<property name="hibernate.c3p0.contextClassLoaderSource">library</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.intu.Dashboard" />
<mapping class="com.intu.Employee"/>
<mapping class="com.intu.Person"/>
The above code is working, but not by second level cache, each time its hitting DB.
What steps do I need to do to get it done? Is there any other way
Please call cr.setCacheable(true) after cr.add(Restrictions.like("userId", id)).
this is my code, where i done mistake. while running Test.java am getting error like "The requested resource is not available". i kept all this files in same package
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/studentdb </property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="connection.pool_size"> 1</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<!-- List of XML mapping files -->
<mapping resource="student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Test.java
public class Test {
public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
cfg.buildSessionFactory();
}
}
student.hbm.xml
<hibernate-mapping>
<class name="com.javathub.Student" table="stu_details">
<id name="id">
<generator class="assigned"></generator>
</id>
<property name="name"></property>
<property name="branch"></property>
<property name="fee"></property>
</class>
</hibernate-mapping>
Student.java
public class Student {
private int id;
private String name;
private String branch;
private double fee;
public Student(){
}
public Student(int id, String name, String branch, double fee) {
super();
this.id = id;
this.name = name;
this.branch = branch;
this.fee = fee;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
public double getFee() {
return fee;
}
public void setFee(double fee) {
this.fee = fee;
}
}
please help me, thanks in advance
keep the student.hbm.xml and hibernate.cfg.xml in src folder not in package and run again.
This question already has answers here:
Hibernate unknown entity (not missing #Entity or import javax.persistence.Entity )
(7 answers)
Closed 7 years ago.
I am getting this below Exception.am using Struts2 and Hibernate for developing web application.can any one help me to resolve this Issue ?
org.hibernate.MappingException: Unknown entity: Pojo.USER [Packagename.entityClassname]
Hibernate.cfg.xml file Content :-
<?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="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/databasename
</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="cache.provider_class">
org.hibernate.cache.HashtableCacheProvider</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
</session-factory>
</hibernate-configuration>
USER's Entity Class content :-
package Pojo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="user")
public class USER {
#Id
#GeneratedValue
#Column(name="USER_ID")
private int id;
#Column(name="USER_NAME")
private String name;
#Column(name="USER_PASSWORD")
private String password;
#Column(name="USER_GENDER")
private String gender;
#Column(name="USER_COUNTRY")
private String country;
#Column(name="USER_ABOUT_YOU")
private String aboutyou;
#Column(name="USER_MAILING_LIST")
private boolean mailinglist;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getAboutyou() {
return aboutyou;
}
public void setAboutyou(String aboutyou) {
this.aboutyou = aboutyou;
}
public boolean isMailinglist() {
return mailinglist;
}
public void setMailinglist(boolean mailinglist) {
this.mailinglist = mailinglist;
}
}
<?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="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/databasename
</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="cache.provider_class">
org.hibernate.cache.HashtableCacheProvider</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<mapping class="Pojo.User">
</session-factory>
</hibernate-configuration>
Use mapping class,so that hibernate will come to know your entity class--> <mapping class="Pojo.User">
USER entity is unknown to your Hibernate session. Add <mapping class="Pojo.USER"/> in session-factory configuration.
...
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<mapping class="Pojo.USER"/>
</session-factory>
</hibernate-configuration>
I have a simple hibernate sample that have an entity class. I add a field to entity class that i don't want to create a column for that field in table of database. how can i do that?
my Entity class:
#Entity
#Table(name = "person")
public class PersonEntity {
private Long id;
private String emailEntity;
private String nameEntity;
private String familyEntity;
private String cityEntity;
private String phoneEntity;
private Long orgId;
private String province;
#Id
#GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Column(name = "email")
public String getEmailEntity() {
return emailEntity;
}
public void setEmailEntity(String emailEntity) {
this.emailEntity = emailEntity;
}
#Column(name = "name")
public String getNameEntity() {
return nameEntity;
}
public void setNameEntity(String nameEntity) {
this.nameEntity = nameEntity;
}
#Column(name = "last_name")
public String getFamilyEntity() {
return familyEntity;
}
public void setFamilyEntity(String familyEntity) {
this.familyEntity = familyEntity;
}
#Column(name = "city")
public String getCityEntity() {
return cityEntity;
}
public void setCityEntity(String cityEntity) {
this.cityEntity = cityEntity;
}
#Column(name = "phone")
public String getPhoneEntity() {
return phoneEntity;
}
public void setPhoneEntity(String phoneEntity) {
this.phoneEntity = phoneEntity;
}
#Column(name = "org_id")
public Long getOrgId() {
return orgId;
}
public void setOrgId(Long orgId) {
this.orgId = orgId;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
hibernate.cfg.xml:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/emailDB</property>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!--<mapping class="net.viralpatel.hibernate.Employee"/>-->
<!--<mapping class="net.viralpatel.hibernate.PersonEntity"/>-->
<mapping class="organizationsTab.PersonEntity"/>
<mapping class="organizationsTab.OrgEntity"/>
</session-factory>
</hibernate-configuration>
'province' field is an auxiliary field and I don't want to create a column for that in table. how can i do that?
Annotate the field with #Transient annotation See more here
Every non static non transient property (field or method depending on the access type) of an entity is considered persistent, unless you annotate it as #Transient.
public transient int counter; //transient property
private String firstname; //persistent property
So you can use #Transient annotation on the property you do not wish to create a column for in database.
Hi Iam trying to run my hiberanate application i am getting the Hibernate Exception.
my hibernate.cfg.xml file is
<?xml version='1.0' encoding='UTF-8'?>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect </property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">mysql</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- <mapping resource="config\\hibernate.hbm.xml"/ -->
<mapping resource="config\\employee.hbm.xml"/>>
</session-factory>
employee.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<hibernate-mapping>
<class name = "com.javatpoint.mypackage.Employee" table = "emp_TPCH" discriminator-value="emp">
<id name = "id">
<generator class = "increment">
</generator>
</id>
<discriminator column="type" type= "string"></discriminator>
<property name ="Name"></property>
<subclass name = "com.javatpoint.mypackage.Regular_Employee" discriminator-value="reg_emp">
<property name = "salary"></property>
<property name = "bonus"></property>
</subclass>
<subclass name = "com.javatpoint.mypackage.Contract_Employee" discriminator-value = "con_emp">
<property name = "pay_Per_Hour"></property>
<property name = "contact_Duration"></property>
</subclass>
</class>
</hibernate-mapping>
Employee.java is
package com.javatpoint.mpackage;
public class Employee {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
}
Contract_Employee.java
package com.javatpoint.mpackage;
public class Contract_Employee extends Employee {
private float pay_Per_Hour;
private String contact_Duration;
public float getPay_Per_Hour() {
return pay_Per_Hour;
}
public void setPay_Per_Hour(float pay_Per_Hour) {
this.pay_Per_Hour = pay_Per_Hour;
}
public String getContact_Duration() {
return contact_Duration;
}
public void setContact_Duration(String contact_Duration) {
this.contact_Duration = contact_Duration;
}
}
Regular Employee.js is
package com.javatpoint.mpackage;
public class Regular_Employee extends Employee {
private float salary;
private int bonus;
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
}
Guys please help me to come out of this Exception.
Include this as the second line in your file:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
There are maybe other errors, but this line is invalid:
<mapping resource="config\\employee.hbm.xml"/>>
^-- two brackets here
Also, the resource should be config/employee.hbm.xml. The resource is a classpath resource, using forward slashes as path separator.