EXAMPLE 1 - Insert
--------------------------------------------------------------------------------------------------
hibernate.cfg.xml
--------------------------------------------------------------------------------------------------
<!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>
<!-- JDBC Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bikesh?useSSL=false</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool settings ... using built-in test pool -->
<property name="connection.pool_size">1</property>
<!-- Select our SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo the SQL to stdout -->
<property name="show_sql">true</property>
<!-- Set the current session context -->
<property name="current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
--------------------------------------------------------------------------------------------------
Programmer.java
--------------------------------------------------------------------------------------------------
package com.demo.spring.entitiy;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="programmer")
public class Programmer {
@Id
@Column(name="id")
int id;
@Column(name="Name")
String name;
@Column(name="email")
String email;
public Programmer(){
}
public Programmer(String name, String email) {
super();
this.name = name;
this.email = email;
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Programmer [id=" + id + ", name=" + name + ", email=" + email
+ "]";
}
}
--------------------------------------------------------------------------------------------------
SpringDemo.java
--------------------------------------------------------------------------------------------------
package com.demo.spring.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.demo.spring.entitiy.Programmer;
public class SpringDemo {
public static void main(String[] args) {
SessionFactory factory=new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Programmer.class)
.buildSessionFactory();
Session session=factory.getCurrentSession();
try{
Programmer programmer=new Programmer("Bikesh Kumar","bikesh.kumar@tiwari.com");
session.beginTransaction();
session.save(programmer);
session.getTransaction().commit();
}
catch(Exception e)
{
e.printStackTrace();
}
finally{
session.close();
}
}
}
No comments:
Post a Comment