Criteria cr=session.createCriteria(Student.class).add(Restrictions.between("strStudentMark1", "90", "100"));
In the above code snippet, I have given the mark between 90 and 100. In the Student table, there are fields between marks 90 and 100. but am not getting those in the output.
What could be the problem? Am I making any mistake?
EDIT
Yes am getting the whole list when I run.
Criteria cr=session.createCriteria(Student.class);
Student entity is below with generated Getters and Setters.
import java.io.Serializable;
public class Student implements Serializable {
private String strStudentID;
private String strStudentRegNO;
private String strStudentName;
private String strStudentMark1;
private String strStudentMark2;
private String strStudentDegree;
private String strStudentMobileNO;
private String strStudentMailID;
private String strSalary;
public String getStrSalary() {
return strSalary;
}
public void setStrSalary(String strSalary) {
this.strSalary = strSalary;
}
public String getStrStudentID() {
return strStudentID;
}
public void setStrStudentID(String strStudentID) {
this.strStudentID = strStudentID;
}
public String getStrStudentRegNO() {
return strStudentRegNO;
}
public void setStrStudentRegNO(String strStudentRegNO) {
this.strStudentRegNO = strStudentRegNO;
}
public String getStrStudentName() {
return strStudentName;
}
public void setStrStudentName(String strStudentName) {
this.strStudentName = strStudentName;
}
public String getStrStudentMark1() {
return strStudentMark1;
}
public void setStrStudentMark1(String strStudentMark1) {
this.strStudentMark1 = strStudentMark1;
}
public String getStrStudentMark2() {
return strStudentMark2;
}
public void setStrStudentMark2(String strStudentMark2) {
this.strStudentMark2 = strStudentMark2;
}
public String getStrStudentDegree() {
return strStudentDegree;
}
public void setStrStudentDegree(String strStudentDegree) {
this.strStudentDegree = strStudentDegree;
}
public String getStrStudentMobileNO() {
return strStudentMobileNO;
}
public void setStrStudentMobileNO(String strStudentMobileNO) {
this.strStudentMobileNO = strStudentMobileNO;
}
public String getStrStudentMailID() {
return strStudentMailID;
}
public void setStrStudentMailID(String strStudentMailID) {
this.strStudentMailID = strStudentMailID;
}
}
hibernate.cfg.xml.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:#aaaa:bbbb</property>
<property name="connection.username">xxx</property>
<property name="connection.password">yyy</property>
<!-- <property name="connection.pool_size">5</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<!-- <property name="hbm2ddl.auto">update</property> -->
<mapping resource="com/hibresources/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Student.hbm.xml.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 17 Dec, 2010 5:54:42 AM by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
<class name="com.bean.Student" table="STUDENT">
<meta attribute="class-description">
This class has details abt Students
</meta>
<id name="strStudentID" >
<column name="STUID" />
</id>
<property name="strStudentRegNO" >
<column name="STUREG_NO" />
</property>
<property name="strStudentName" >
<column name="STUNAME" />
</property>
<property name="strStudentMark1" >
<column name="STUMARK1" />
</property>
<property name="strStudentMark2" >
<column name="STUMARK2" />
</property>
<property name="strStudentDegree" >
<column name="DEGREE" />
</property>
<property name="strStudentMobileNO" >
<column name="MOBILENO" />
</property>
<property name="strStudentMailID" >
<column name="MAILID" />
</property>
<property name="strSalary" >
<column name="SALARY" />
</property>
</class>
</hibernate-mapping>
Please make sure that the data stored in the Database is an INT. The between will only work with INT Datatype. Also try passing the value as int in code.
Related
I'm working for several hours on this issue about hibernate mapping. I think the error could be a simple syntax mistake but I can't find it!!!
I ran into the following exception when I executed my code:
Initial SessionFactory creation failed.org.hibernate.MappingException: entity class not found: LegalFee
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.plm.dao.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:33)
at com.plm.dao.util.HibernateUtil.getSessionFactory(HibernateUtil.java:39)
at com.plm.dao.AppTest.main(AppTest.java:15)
Firstly, I'm using hibernate 4.2 with Java 8 and MariaDB 10.
See all configurations below.
My hibernate.cfg.xml, I removed C3P0 configuration:
<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/PokerLeagueManager</property>
<property name='connection.username'>root</property>
<property name='connection.password'>mypassword</property>
<property name="show_sql">true</property>
<!-- SQL dialect -->
<property name='dialect'>org.hibernate.dialect.MySQL5InnoDBDialect</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.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name='show_sql'>true</property>
<mapping resource="mappings/BlindStructure.hbm.xml"/>
<mapping resource="mappings/Tournament.hbm.xml"/>
<mapping resource="mappings/LegalFee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
The error is on LegalFee bean so focus on it.
See the sql table creation of legalFee:
CREATE TABLE `legalFee` (
`idFee` int(11) NOT NULL,
`shortName` varchar(50) NOT NULL,
`description` varchar(200) DEFAULT NULL,
`feePercent` int(11) DEFAULT NULL,
`feeFixed` int(11) DEFAULT NULL,
PRIMARY KEY (`idFee`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The class :
public class LegalFee implements Serializable {
/**
* generated serial uid
*/
private static final long serialVersionUID = -2259355205530727294L;
private int idFee;
private String shortName;
private String description;
private Integer feePercent;
private Integer feeFixed;
private Set<Tournament> tournaments = new HashSet<Tournament>(0);
public LegalFee() {
}
public LegalFee(int idFee, String shortName) {
this.idFee = idFee;
this.shortName = shortName;
}
public LegalFee(int idFee, String shortName, String description,
Integer feePercent, Integer feeFixed, Set<Tournament> tournaments) {
this.idFee = idFee;
this.shortName = shortName;
this.description = description;
this.feePercent = feePercent;
this.feeFixed = feeFixed;
this.tournaments = tournaments;
}
public int getIdFee() {
return this.idFee;
}
public void setIdFee(int idFee) {
this.idFee = idFee;
}
public String getShortName() {
return this.shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getFeePercent() {
return this.feePercent;
}
public void setFeePercent(Integer feePercent) {
this.feePercent = feePercent;
}
public Integer getFeeFixed() {
return this.feeFixed;
}
public void setFeeFixed(Integer feeFixed) {
this.feeFixed = feeFixed;
}
public Set<Tournament> getTournaments() {
return this.tournaments;
}
public void setTournaments(Set<Tournament> tournaments) {
this.tournaments = tournaments;
}
}
And lastly the LegalFee.hbm.xml:
<?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="LegalFee" table="legalFee" catalog="PokerLeagueManager" optimistic-lock="version">
<id name="idFee" type="int">
<column name="idFee" />
<generator class="identity" />
</id>
<property name="shortName" type="string">
<column name="shortName" length="50" not-null="true" />
</property>
<property name="description" type="string">
<column name="description" length="200" />
</property>
<property name="feePercent" type="java.lang.Integer">
<column name="feePercent" />
</property>
<property name="feeFixed" type="java.lang.Integer">
<column name="feeFixed" />
</property>
<set name="tournaments" table="tournament" inverse="true" lazy="true" fetch="select">
<key>
<column name="legalFee_feeId" />
</key>
<one-to-many class="Tournament" />
</set>
</class>
</hibernate-mapping>
Thanks for your help.
If your LegalFee class is not in default package you need to provide the fully classified name of the class.
e.g "class name="com.abc.xyx.LegalFee" table="legalFee" "
The exception is about table not mapping to class, however I failed to debug what went wrong.
I have checked the xml configurations. the class is UserType and the database table is called UserType as well.
UserType.java:
package com.howtodoinjava.entity;
public class UserType {
private int _userTypeID;
private String _userTypeName;
private boolean _typeIsSuperUser;
public int getUserTypeID() { return _userTypeID; }
public String getUserTypeName() {return _userTypeName; }
public boolean getTypeIsSuperUser() {return _typeIsSuperUser; }
public UserType setUserTypeID(int id) { _userTypeID = id; return this; }
public UserType setUserTypeName(String name) { _userTypeName = name; return this; }
public UserType setTypeIsSuperUser(boolean isSuperUser) { _typeIsSuperUser = isSuperUser; return this; }
}
UserTypeDAOImpl.java:
package com.howtodoinjava.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.howtodoinjava.entity.UserType;
#Repository
public class UserTypeDAOImpl implements UserTypeDAO {
#Autowired
private SessionFactory sessionFactory;
public void add(UserType ut) {
this.sessionFactory.getCurrentSession().save(ut);
}
#SuppressWarnings("unchecked")
public List<UserType> getAll() {
return this.sessionFactory.getCurrentSession().createQuery("from UserType").list();
}
public void delete(Integer id) {
UserType ut= (UserType) sessionFactory.getCurrentSession().load(
UserType.class, id);
if (null != ut) {
this.sessionFactory.getCurrentSession().delete(ut);
}
}
}
EditUserTypeController.java:
#Controller
public class EditUserTypeController{
#Autowired
private UserTypeManager userTypeManager;
#RequestMapping(value = "/", method = RequestMethod.GET)
public String list(ModelMap map)
{
map.addAttribute("UserType", new UserType());
map.addAttribute("UserTypeList", userTypeManager.getAll());
return "editEUserTypeList";
}
#RequestMapping(value = "/add", method = RequestMethod.POST)
public String addUserType(#ModelAttribute(value="ut") UserType ut, BindingResult result)
{
userTypeManager.add(ut);
return "redirect:/";
}
#RequestMapping("/delete/{id}")
public String deleteUserType(#PathVariable("id") Integer id)
{
userTypeManager.delete(id);
return "redirect:/";
}
public void setUserTypeManager(UserTypeManager new_utm) {
this.userTypeManager = new_utm;
}
}
my UserType-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<context:annotation-config />
<context:component-scan base-package="com.howtodoinjava.controller" />
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="UserTypeDAO" class="com.howtodoinjava.dao.UserTypeDAOImpl"></bean>
<bean id="UserTypeManager" class="com.howtodoinjava.service.UserTypeManagerImpl"> </bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping resource="UserType.hbm.xml" />
</session-factory>
</hibernate-configuration>
UserType.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.howtodoinjava.entity.UserType" table="UserType">
<meta attribute="class-description">
user type
</meta>
<id name="_userTypeID" column="UserTypeId" type="int"> <generator class="native"/> </id>
<property name="_userTypeName" column="UserTypeName" type="string"/>
<property name="_typeIsSuperUser" column="TypeIsSuperUser" type="string"/>
</class>
</hibernate-mapping>
I appreciate any helps!
The problem is in hibernate mapping configuration:
name: should be same as class field name and
column: is the name of database table column, or the column name to be set while table creation by hibernate.
Note: The mapping file instructs Hibernate how to map the defined class to the database table.
your UserType.hbm.xml should be look like:
<hibernate-mapping>
<class name="com.howtodoinjava.entity.UserType" table="UserType">
<meta attribute="class-description">
user type
</meta>
<id name="_userTypeID" column="userTypeID" type="int">
<generator class="native"/>
</id>
<property name="_userTypeName" column="userTypeName" type="string"/>
<property name="_typeIsSuperUser" column="typeIsSuperUser" type="string"/>
</class>
</hibernate-mapping>
It turned out this is related to the HQL issue: instead of using from UserType, use from com.howtodojava.entity.UserType;
and the error message will be gone.
I made a connection to SQL server with Hibernate configuration wizard, when I click test connection, I get success message, but when I want to make hibernate mapping, I cannot I get error message, Cannot established a connection to jdbc. The below code is jdbc connection with hibernate; please say me, what is the problem?
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost; databaseName=Test</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">sa123</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
</session-factory>
</hibernate-configuration>
Best Regards
After looking into your code i found you did not specify the port number for database connection
<property name="hibernate.connection.url">jdbc:sqlserver://localhost; databaseName=Test</property>
There should be a port number if your trying to connect on your local machine for an example please refer following line
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=syv</property>
Please check and let me know ...if there is any further issue
Ok here i am sending you the basic hibernate configuration : -
STEP 1 :- *Create a Java File “AddStudent” and paste the below code into that.*
//package code;
import java.sql.*;
import java.io.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class AddStudent {
private static SessionFactory sessionFactory;
public static void main(String args[]) throws Exception {
DataInputStream d = new DataInputStream(System.in);
System.out.println("ENTER YOUR NAME");
String name = d.readLine();
System.out.println("ENTER YOUR DEGREE");
String degree = d.readLine();
System.out.println("ENTER YOUR PHONE");
String phone = d.readLine();
System.out.println("Name: " + name);
System.out.println("Degree: " + degree);
System.out.println("Phone: " + phone);
if ((name.equals("") || degree.equals("") || phone.equals(""))) {
System.out.println("All informations are Required");
} else {
try {
// begin try
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Exception e) {
System.out.println(e.getMessage());
System.err.println("Initial SessionFactory creation failed."
+ e);
}
Session s = sessionFactory.openSession();
Transaction tx = s.beginTransaction();
Student stu = new Student();
stu.setName(name);
stu.setDegree(degree);
stu.setPhone(phone);
s.save(stu);
tx.commit();
System.out.println("Added to Database");
if (s != null)
s.close();
}
}
}
STEP 2 : Create a Java File “Student” paste the below code into that.
//package code;
import java.io.*;
public class Student implements Serializable {
private long id;
private String name;
private String degree;
private String phone;
public long getId() {
return id;
}
public String getName() {
return name;
}
public String getDegree() {
return degree;
}
public String getPhone() {
return phone;
}
public void setId(long string) {
id = string;
}
public void setName(String string) {
name = string;
}
public void setDegree(String string) {
degree = string;
}
public void setPhone(String string) {
phone = string;
}
public String toString() {
return name;
}
}
Step 3: Create an xml file “hibernate.cfg.xml” place the below code into that.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="studentFactory">
<property name="connection.driver_class">
oracle.jdbc.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:#localhost:1521:XE
</property>
<property name="connection.username">
system
</property>
<property name="connection.password">
system
</property>
<property name="connection.pool_size">5</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.OracleDialect
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<mapping resource="Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
Step 4:Create an xml File “student.hbm.xml” and place below code into that.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="Student" table="Stutbl" >
<id name="id" type="long" column ="ID">
<generator class="increment"/>
</id>
<property name="name" column="name" not-null="true"/>
<property name="degree" column="degree" />
<property name="phone" column="phone" />
</class>
</hibernate-mapping>
Step 5: Add the required jar to your project
Hope it will solve your problem
Add this:
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
and verify if your Database accept connections.
I am using Hibernate4 with Spring and I am trying to map a Term class to a Document class. A term may occur in many documents and a document can contain many terms. I have read up on this, and I have attempted a number of fixes, including overriding the equals and hashCode methods.
In my database the terms and documents are stored in separated tables mediated by a relationship table. In the code here, it is called the jtable. I am using hibernates many-to-many mapping, but every time that I try to fetch a term object it simply deletes the row in the relationship table.
Here is a sample sql output that hibernate's show sql is giving out:
Hibernate: SELECT * FROM tags WHERE token='rose'
Hibernate: delete from jtable where tag_id=?
Here are the relevant classes and configs:
public class Term {
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Set<Document> getDocuments() {
return documents;
}
public void setDocuments(Set<Document> documents) {
documents = documents;
}
#Override
public boolean equals(Object obj) {
if(obj.getClass() != getClass()){
return false;
}
Term term = (Term) obj;
if (hashCode() == term.hashCode()){
return true;
}
return false;
}
#Override
public int hashCode(){
return word.hashCode();
}
private String word;
private int id;
private Set<Document> documents = new HashSet<Document>();
}
And the Document class :
public class Document implements Comparable<Document> {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public URL getUrl() {
return url;
}
public void setUrl(URL url) {
this.url = url;
}
public Integer getLength() {
return length;
}
public void setLength(Integer length) {
this.length = length;
}
public Double getRank() {
return rank;
}
public void setRank(Double rank) {
this.rank = rank;
}
#Override
public boolean equals(Object obj) {
if(obj.getClass() != getClass()){
return false;
}
Document document = (Document) obj;
if (hashCode() == document.hashCode()){
return true;
}
return false;
}
#Override
public int hashCode(){
int hash = 1;
hash *= 1 + (length == null ? 0 :length);
hash *= 2 + (rank == null ? 0 :rank);
hash *= 3 + (title == null ? 0 : title.hashCode());
hash *= 4 + (url.hashCode());
return hash;
}
#Override
public int compareTo(Document document) {
return rank.compareTo(document.getRank());
}
private int id;
private Integer length;
private Double rank;
private String title;
private URL url;
}
Document.hbm.xml
<?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>
<class name="package.Document" table="documents">
<id name="id" type="java.lang.Integer">
<column name="document_id" />
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="title" length="255" not-null="false" />
</property>
<property name="length">
<column name="length" not-null="false"/>
</property>
<property name="rank" type="java.lang.Double">
<column name="rank" not-null="false" />
</property>
<property name="url" type="org.hibernate.type.UrlType">
<column name="url" length="255" not-null="false" />
</property>
</class>
</hibernate-mapping>
Term.hbm.xml
<?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>
<class name="package.Term" table="tags">
<id name="id" type="java.lang.Integer">
<column name="tag_id" />
<generator class="native" />
</id>
<property name="word" type="string">
<column name="token" not-null="true" unique="true" />
</property>
<set name="documents" table="jtable"
inverse="false" lazy="true" fetch="select" cascade="all" >
<key>
<column name="tag_id" not-null="true" />
</key>
<many-to-many entity-name="package.Document">
<column name="document_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
And the relevant context config section:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.user}"/>
<property name="password" value="${database.password}"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>Document.hbm.xml</value>
<value>Term.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
I have been looking at this error for a while, so any answers would be appreciated.
This is my first post in stackoverflow:
I am new to hibernate, infact below is my first code in hibernate:
i am repetedly getting "could not parse mapping document" error, when i run my main class.
below is the code for your refernce, please help me out of this.
Config 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.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:1527/sample</property>
<property name="hibernate.connection.username">one</property>
<property name="hibernate.connection.password">two</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="EmployeeMapping.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Mapping 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="EMPLOYEE"/>
<id name="emp_id" type="int" column="emp_id"/>
<property name="emp_name">
<column name="emp_name"/>
</property>
<property name="emp_branch">
<column name="emp_branch"/>
</property>
<property name="emp_Salary">
<column name="emp_salary"/>
</property>
<property name="emp_fav_dgt">
<column name="emp_fav_dgt"/>
</property>
</hibernate-mapping>
Pojo class:
public class Employee {
private int emp_id;
private String emp_name;
private String emp_branch;
private int emp_Salary;
private int emp_fav_dgt;
public int getEmp_Salary() {
return emp_Salary;
}
public void setEmp_Salary(int emp_Salary) {
this.emp_Salary = emp_Salary;
}
public String getEmp_branch() {
return emp_branch;
}
public void setEmp_branch(String emp_branch) {
this.emp_branch = emp_branch;
}
public int getEmp_fav_dgt() {
return emp_fav_dgt;
}
public void setEmp_fav_dgt(int emp_fav_dgt) {
this.emp_fav_dgt = emp_fav_dgt;
}
public int getEmp_id() {
return emp_id;
}
public void setEmp_id(int emp_id) {
this.emp_id = emp_id;
}
public String getEmp_name() {
return emp_name;
}
public void setEmp_name(String emp_name) {
this.emp_name = emp_name;
}
}
Main class:
public class Main {
public static void main(String[] args) {
System.out.println("asdf");
SessionFactory sessionFactory = null;
Transaction ts = null;
Session session = null;
Configuration conf = null;
try{
conf = new org.hibernate.cfg.Configuration().configure();
sessionFactory = conf.buildSessionFactory();
session =sessionFactory.openSession();
Employee customer = new Employee();
customer.setEmp_id(1);
customer.setEmp_name("one");
customer.setEmp_branch("old");
customer.setEmp_Salary(1000);
customer.setEmp_fav_dgt(2);
session.save(customer);
ts.commit();
}
catch(Exception e){
e.printStackTrace();;
}
finally{
session.close();
}
}
}
The property tags corresponding with the fields should be nested within the class tag.
<class name="Employee" table="EMPLOYEE">
<id name="emp_id" type="int" column="emp_id"/>
<property name="emp_name">
<column name="emp_name"/>
</property>
<property name="emp_branch">
<column name="emp_branch"/>
</property>
<property name="emp_Salary">
<column name="emp_salary"/>
</property>
<property name="emp_fav_dgt">
<column name="emp_fav_dgt"/>
</property>
</class>