Hibernate: Cannot open connection - java

I am getting message cannot open connection,nothing specific log is being generated too
connection cannot open
and my hibernate.cfg.xml is:-
<!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/hibernate_oar</property>
<property name="hibernate.connection.user"> root</property>
<property name="hibernate.connection.password">dinga</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="use_sql_comments">true</property>
<property name="hibernate.validator.apply_to_ddl">false</property>
<property name="hibernate.validator.autoregister_listeners">false</property>
<property name="minPoolSize"> 5</property>
<property name="maxPoolSize">100</property>
<property name="initialPoolSize">5</property>
<property name="validateConnectionOnBorrow">true</property>
<property name="maxStatements">10</property>
<mapping class="com.model.Student" />
</session-factory>
I am generating session
public class DataAccessObject {
private static Session session = null;
public static Session getSession() {
AnnotationConfiguration cfg = new AnnotationConfiguration().configure();
//cfg.addAnnotatedClass(hello.Message.class);
SessionFactory annotedSessionFactory = cfg.buildSessionFactory();
Session session = annotedSessionFactory.openSession();
return session;
}
}
and I am using session to retrieve data from mysql database
public class CrudDao {
private Session session;
public CrudDao() {
session = DataAccessObject.getSession();
}
public List<Student> getAllStudents() {
Transaction tx = null;
Query query = null;
List<Student> students = new ArrayList();
String myquery = "select s.studentId,s.name,s.department,s.emailId,s.status from Student s";
try {
query = session.createQuery(myquery);
List<Student> list = query.list();
System.out.println(list);
Iterator<Student> itr = list.iterator();
Student newstudent = new Student();
while (itr.hasNext()) {
Student mystudent = itr.next();
newstudent.setStudentId(mystudent.getStudentId());
newstudent.setName(mystudent.getName());
newstudent.setDepartment(mystudent.getDepartment());
newstudent.setEmailId(mystudent.getEmailId());
newstudent.setStatus(mystudent.getStatus());
students.add(newstudent);
System.out.println(students);
}
} catch (HibernateException e) {
System.err.println(e.getMessage());
} finally {
session.close();
}
return students;
}
}

Related

Hibernate SQLGrammarException: could not prepare statement

I have simple project with one class and a corresponding table in the database (MySQL). It works fine if put the hbm2ddl.auto set create in the hibernate configuration file. But, if I take it out. The code throws the following exception:
ERROR: Table "STUDENT" not found; SQL statement:
into student (fname, lname, entrance) values (?, ?, ?) [42102-185]
org.hibernate.exception.SQLGrammarException: could not prepare statement
I have already created the table in database but I don't know why hibernate cannot find the table. I am not sure how the connection to the database works.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/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>
<!-- Assume test is the database name -->
<property name="hibernate.CONNECTION.url">
jdbc:mysql://localhost:3306/sample;MVCC=TRUE;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;SCHEMA=sample
</property>
<property name="hibernate.connection.username"> root </property>
<property name="hibernate.connection.password"> **** </property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- -->
<property name="hbm2ddl.auto">create</property>
<!-- List of XML mapping files -->
<!-- -->
<mapping resource="Student.hbm.xml" />
<!-- <mapping class="ourPackage.Student" /> -->
</session-factory>
</hibernate-configuration>
Student Class:
#Entity
#Table(name="student")
public class Student implements Serializable{
public Student(){
}
public Student(int id, String fName, String lName, String ent){
this.id = id;
this.fname = fName;
this.lname = lName;
this.entrance = ent;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEntrance() {
return entrance;
}
public void setEntrance(String entrance) {
this.entrance = entrance;
}
#Id
#GeneratedValue
private int id;
private String fname;
private String lname;
private String entrance;
}
Student hibernate config
<hibernate-mapping>
<class name="ourPackage.Student" table="student">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="fname" column="fname" type="string"/>
<property name="lname" column="lname" type="string"/>
<property name="entrance" column="entrance" type="string"/>
</class>
</hibernate-mapping>
Main class
public class StudentDB {
private static SessionFactory factory;
private static ServiceRegistry serviceRegistry;
#SuppressWarnings("deprecation")
public static void main(String[] args) {
try {
// factory = new Configuration().configure().buildSessionFactory();
createSessionFactory();
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
StudentDB ME = new StudentDB();
Integer empID1 = ME.addStudent("Alex", "Jason", "2005");
ME.listStudent();
factory.close();
}
public void listStudent() {
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query temp = session.createQuery("from Student");
List students = temp.list();
for (Iterator iterator = students.iterator(); iterator.hasNext();) {
Student student = (Student) iterator.next();
System.out.print("First Name: " + student.getFname());
System.out.print(" Last Name: " + student.getLname());
System.out.println(" entrance: " + student.getEntrance());
}
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
public static SessionFactory createSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
factory = configuration.buildSessionFactory(serviceRegistry);
return factory;
}
}

Hibernate postgres - relation "userdetails" does not exist

I'm just starting with hibernate and I have a simple configuration that gives me this error: "relation "userdetails" does not exist" when I try to first save the only class I have.
my hibernate.gfg.xml file:
<!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">org.postgresql.Driver</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">XXXX</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.PostgreSQLDialect
</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hdm2ddl.auto">create</property>
<mapping class="hibernate.project.UserDetails"/>
</session-factory>
</hibernate-configuration>
my only model class:
#Entity
public class UserDetails {
#Id
private int userId;
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
my main:
public static void main(String[] args) {
UserDetails user = new UserDetails();
user.setUserId(1);
user.setUserName("First User");
Configuration config = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
any ideas? I've found similar questions but didn't help. thanks!
The create property is incorrect : Use hbm2ddl instead of hdm2ddl
<property name="hibernate.hbm2ddl.auto">create</property>
OR
<property name="hbm2ddl.auto">create</property>
and not
hdm2ddl.auto
Seems to be typo at your end.

Can not established a connection to jdbc with Hibernate-SQL Server 2008

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.

Exception in thread "main" org.hibernate.MappingException: Unknown entity:

I'm using myeclipse IDE
After executing my code i'm getting the below Exception
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.MappingException: Unknown entity:info.inetsolv.Emp
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister
(SessionFactoryImpl.java:628)
at org.hibernate.impl.SessionImpl.getEntityPersister
(SessionImpl.java:1366)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState
(AbstractSaveEventListener.java:535)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist
(DefaultPersistEventListener.java:93)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist
(DefaultPersistEventListener.java:61)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:646)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:620)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:624)
at info.inetsolv.InsertEmprecord.main(InsertEmprecord.java:22)
POJO CLASS
package info.inetsolv;
#SuppressWarnings("serial")
public class Emp implements java.io.Serializable {
// Fields
private Integer eno;
private String name;
private Double salary;
// Constructors
/** default constructor */
public Emp() {
}
/** minimal constructor */
public Emp(Integer eno) {
this.eno = eno;
}
/** full constructor */
public Emp(Integer eno, String name, Double salary) {
this.eno = eno;
this.name = name;
this.salary = salary;
}
// Property accessors
public Integer getEno() {
return this.eno;
}
public void setEno(Integer eno) {
this.eno = eno;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return this.salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
}
HibernateSessionFactory.java
package info.inetsolv;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static final ThreadLocal<Session> threadLocal = new
ThreadLocal<Session>();
private static org.hibernate.SessionFactory sessionFactory;
private static Configuration configuration = new Configuration();
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession(): null;
threadLocal.set(session);
}
return session;
}
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
public static Configuration getConfiguration() {
return configuration;
}
}
client program to insert record into DB
InsertEmprecord.java
package info.inetsolv;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class InsertEmprecord {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session hsession = sf.openSession();
Transaction tx = hsession.beginTransaction();
Emp e = new Emp();
e.setEno(6);
e.setName("six");
e.setSalary(1234d);
hsession.persist(e);
tx.commit();
hsession.close();
sf.close();
}
}
And below is my hibernate mapping file
Emp.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="info.inetsolv.Emp" table="EMP" schema="HIB">
<id name="eno" type="java.lang.Integer">
<column name="ENO" precision="5" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="10" />
</property>
<property name="salary" type="java.lang.Double">
<column name="SALARY" precision="10" />
</property>
</class>
</hibernate-mapping>
AND below is my hibernate configuration file
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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:#localhost:1521:xe
</property>
<property name="connection.username">hib</property>
<property name="connection.password">abc</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="myeclipse.connection.profile">
my oracle drive
</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
You didn't configure mapping for the object Emp. The configuration file hibernate.cfg.xml should contain the mapping to the resource Emp.hbm.xml.
<mapping resource="info/inetsolv/Emp.hbm.xml"/>
I had similar problem for a simple Console application trying to use Hibernate. The solution I arrived to make the add the "packagesToScan" property explicitly for LocalSessionFactoryBean.
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.mg.learning.spring.orm"/> <--- this SOLVED!
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
#Sandhu Santakumar , answer is absolutely right.
Just adding the reason behind this.
By default the JBoss hibernate reverse engineering tool maps class inside the mapping tab but resource attribute is the required attribute that hibernate.cfg.xml should have.
class attribute is optional.
e.g. if your mapping is like this
resource is mandatory attribute and class is optional attribute.
Hope this additional information helps.

Could not parse mapping document error in hibernate

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>

Categories