Add following dependency in pom.xml file.
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.28.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
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.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/stds</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
Student.java
package com.deepsingh44.model;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
@Entity
public class Student {
@EmbeddedId
private StudentKey studentKey;
private String name;
private String address;
public StudentKey getStudentKey() {
return studentKey;
}
public void setStudentKey(StudentKey studentKey) {
this.studentKey = studentKey;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
StudentKey.java
package com.deepsingh44.model;
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class StudentKey implements Serializable {
private String mobile;
private String email;
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Test.java
package com.deepsingh44.model;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class Test {
public static void main(String[] args) {
// Load your configuration file
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().
configure("hibernate.cfg.xml").
build();
Metadata meta = new MetadataSources(ssr).
addAnnotatedClass(Student.class).
getMetadataBuilder().
build();
// Get sessionFactory object
SessionFactory factory = meta.
getSessionFactoryBuilder().
build();
// Get session object
Session session = factory.openSession();
Student s=new Student();
s.setAddress("abc");
s.setName("A");
StudentKey sk=new StudentKey();
sk.setEmail("98XXXXXXXX");
sk.setMobile("d@gmail.com");
s.setStudentKey(sk);
session.save(s);
session.beginTransaction().commit();
System.out.println("success");
}
}
No comments:
Post a Comment