Composite Primary Keys Using Embeddable In Hibernate | Code Factory
Download code and jars : Link.
File : UserId.java
package com.codeFactory.bean;
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class UserId implements Serializable {
public class UserId implements Serializable {
String firstName;
String lastName;
String lastName;
public String getFirstName() {
return firstName;
}
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
this.lastName = lastName;
}
}
File : User.java
package com.codeFactory.bean;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
@Entity
public class User {
public class User {
@EmbeddedId
@AttributeOverride(name = "firstName", column = @Column(name = "fid_firstname"))
UserId userId;
@AttributeOverride(name = "firstName", column = @Column(name = "fid_firstname"))
UserId userId;
Integer age;
public UserId getUserId() {
return userId;
}
return userId;
}
public void setUserId(UserId userId) {
this.userId = userId;
}
this.userId = userId;
}
public Integer getAge() {
return age;
}
return age;
}
public void setAge(Integer age) {
this.age = age;
}
this.age = age;
}
}
File : Action.java
package com.codeFactory.action;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import com.codeFactory.bean.User;
import com.codeFactory.bean.UserId;
import com.codeFactory.bean.UserId;
public class Action {
static AnnotationConfiguration annotationConfiguration;
static SessionFactory sessionFactory;
static SessionFactory sessionFactory;
public static void main(String[] args) {
annotationConfiguration = new AnnotationConfiguration();
sessionFactory = annotationConfiguration.configure("hibernate.cfg.xml")
.buildSessionFactory();
sessionFactory = annotationConfiguration.configure("hibernate.cfg.xml")
.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = null;
Transaction transaction = null;
try {
transaction = session.beginTransaction();
User user = new User();
user.setAge(25);
transaction = session.beginTransaction();
User user = new User();
user.setAge(25);
UserId userId = new UserId();
userId.setFirstName("first Two");
userId.setLastName("last");
userId.setFirstName("first Two");
userId.setLastName("last");
user.setUserId(userId);
session.save(user);
transaction.commit();
System.out.println("Saved.");
session.close();
} catch (Exception e) {
System.out.println("Exception : " + e);
}
}
session.save(user);
transaction.commit();
System.out.println("Saved.");
session.close();
} catch (Exception e) {
System.out.println("Exception : " + e);
}
}
}
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">
<!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="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping class="com.codeFactory.bean.User" />
</session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping class="com.codeFactory.bean.User" />
</session-factory>
</hibernate-configuration>
Comments
Post a Comment