In Addition to Previous simple example:
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/masterjee</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.akt.User"/>
<mapping class="com.akt.Role"/>
</session-factory>
</hibernate-configuration>
User.java
package com.akt;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue
@Column(name = "userId", unique = true, nullable = false)
private int userId;
@Column(name = "name")
private String name;
@Column(name = "email")
private String email;
@Column(name = "password")
private String password;
@Column(name = "age")
private int age;
@Column(name = "salary")
private long salary;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "userId")
private List<Role> roles;
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
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;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
}
Add new class Role.java
package com.akt;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "role")
public class Role {
@Id
@GeneratedValue
@Column(name = "roleId", unique = true, nullable = false)
private int roleId;
@Column(name = "roleName")
private String roleName;
@ManyToOne
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int getRoleId() {
return roleId;
}
public void setRoleId(int roleId) {
this.roleId = roleId;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
}
MainClass.java
package com.akt;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
public class MainClass {
public static void main(String[] args) {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.getTransaction();
transaction.begin();
User user = new User();
user.setName("Anil");
user.setEmail("anil.thakur0909@gmail.com");
user.setPassword("test123");
user.setSalary(1500);
user.setAge(28);
Role role1 = new Role();
role1.setRoleName("Developer");
Role role2 = new Role();
role2.setRoleName("Developer");
List<Role> roles = new ArrayList<Role>();
roles.add(role1);
roles.add(role2);
user.setRoles(roles);
session.persist(user);
transaction.commit();
session.close();
sessionFactory.close();
}
}
No comments:
Post a Comment