Hibernate Mapping and Many to One example - part 2
This is a continuation of Hibernate Mapping and Many to One example - part 1.
Step 5: Define the previously mapped classes UserApp and UserAppId in persistence.xml file along with the other classes that will be mapped below.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="myapp">
<class>com.jpmorgan.myapp.model.security.CmnUser</class>
<class>com.jpmorgan.myapp.model.security.UserApp</class>
<class>com.jpmorgan.myapp.model.security.UserAppId</class>
<class>com.jpmorgan.myapp.model.security.User</class>
<class>com.jpmorgan.myapp.model.security.App</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.SybaseASE15Dialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.cache.use_second_level_cache"
value="false" />
</properties>
</persistence-unit>
</persistence>
Step 6: Mapping of User table.
package com.myapp.dm.model.security;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
@SuppressWarnings("serial")
@Entity
@Table(name = "User", schema = "dbo", catalog = "my_app")
public class User implements java.io.Serializable
{
private String userName;
private char disabled;
private String comment;
private char status;
private Date createdOn;
private String createdBy;
private Date modifiedOn;
private String modifiedBy;
private byte[] timestamp;
public User()
{
}
public User(String userName,char disabled, char status, Date createdOn, String createdBy,
Date modifiedOn, String modifiedBy)
{
this.userName = userName;
this.disabled = disabled;
this.status = status;
this.createdOn = createdOn;
this.createdBy = createdBy;
this.modifiedOn = modifiedOn;
this.modifiedBy = modifiedBy;
}
@Id
@Column(name = "UserName", unique = true, nullable = false, length = 40)
public String getUserName()
{
return this.userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
@Column(name = "UserType", nullable = false, length = 40)
public String getUserType()
{
return this.userType;
}
public void setUserType(String userType)
{
this.userType = userType;
}
@Column(name = "Disabled", nullable = false, length = 1)
public char getDisabled()
{
return this.disabled;
}
public void setDisabled(char disabled)
{
this.disabled = disabled;
}
@Column(name = "Comment")
public String getComment()
{
return this.comment;
}
public void setComment(String comment)
{
this.comment = comment;
}
@Column(name = "Status", nullable = false, length = 1)
public char getStatus()
{
return this.status;
}
public void setStatus(char status)
{
this.status = status;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CreatedOn", nullable = false, length = 23)
public Date getCreatedOn()
{
return this.createdOn;
}
public void setCreatedOn(Date createdOn)
{
this.createdOn = createdOn;
}
@Column(name = "CreatedBy", nullable = false, length = 40)
public String getCreatedBy()
{
return this.createdBy;
}
public void setCreatedBy(String createdBy)
{
this.createdBy = createdBy;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "ModifiedOn", nullable = false, length = 23)
public Date getModifiedOn()
{
return this.modifiedOn;
}
public void setModifiedOn(Date modifiedOn)
{
this.modifiedOn = modifiedOn;
}
@Column(name = "ModifiedBy", nullable = false, length = 40)
public String getModifiedBy()
{
return this.modifiedBy;
}
public void setModifiedBy(String modifiedBy)
{
this.modifiedBy = modifiedBy;
}
@Version
@Column(name = "Timestamp", insertable = false, updatable = false, unique = true, nullable = false)
@Generated(GenerationTime.ALWAYS)
public byte[] getTimestamp()
{
return this.timestamp;
}
public void setTimestamp(byte[] timestamp)
{
this.timestamp = timestamp;
}
}
Step 7: Mapping of App table.
package com.myapp.model.security;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;
@SuppressWarnings("serial")
@Entity
@Table(name = "App", schema = "dbo", catalog = "sne_kath")
public class App implements java.io.Serializable
{
private String appCd;
private String description;
private char status;
private char disabled;
private Date createdOn;
private String createdBy;
private Date modifiedOn;
private String modifiedBy;
private byte[] timestamp;
private String requiresDeskTop;
public App()
{
}
public App(String appCd, String description, char status, Date createdOn, String createdBy, Date modifiedOn,
String modifiedBy)
{
this.appCd = appCd;
this.description = description;
this.status = status;
this.createdOn = createdOn;
this.createdBy = createdBy;
this.modifiedOn = modifiedOn;
this.modifiedBy = modifiedBy;
}
@Column(name = "appcd", nullable = false)
@Id
public String getAppCd()
{
return appCd;
}
public void setAppCd(String appCd)
{
this.appCd = appCd;
}
@Column(name = "description", nullable = false)
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public char getDisabled()
{
return disabled;
}
public void setDisabled(char disabled)
{
this.disabled = disabled;
}
@Column(name = "RequiresDesktop", nullable = true)
public String getRequiresDeskTop()
{
return requiresDeskTop;
}
public void setRequiresDeskTop(String requiresDeskTop)
{
this.requiresDeskTop = requiresDeskTop;
}
@Version
@Column(name = "Timestamp", insertable = false, updatable = false, unique = true, nullable = false)
@Generated(GenerationTime.ALWAYS)
public byte[] getTimestamp()
{
return this.timestamp;
}
public void setTimestamp(byte[] timestamp)
{
this.timestamp = timestamp;
}
@Column(name = "Status", nullable = false, length = 1)
public char getStatus()
{
return this.status;
}
public void setStatus(char status)
{
this.status = status;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CreatedOn", nullable = false, length = 23)
public Date getCreatedOn()
{
return this.createdOn;
}
public void setCreatedOn(Date createdOn)
{
this.createdOn = createdOn;
}
@Column(name = "CreatedBy", nullable = false, length = 40)
public String getCreatedBy()
{
return this.createdBy;
}
public void setCreatedBy(String createdBy)
{
this.createdBy = createdBy;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "ModifiedOn", nullable = false, length = 23)
public Date getModifiedOn()
{
return this.modifiedOn;
}
public void setModifiedOn(Date modifiedOn)
{
this.modifiedOn = modifiedOn;
}
@Column(name = "ModifiedBy", nullable = false, length = 40)
public String getModifiedBy()
{
return this.modifiedBy;
}
public void setModifiedBy(String modifiedBy)
{
this.modifiedBy = modifiedBy;
}
}
Step 8: Finally, the hibernate and JPA dependency jars in Maven's pom.xml file.
Labels: Hibernate, Hibernate Tutorial

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home