Google

Oct 2, 2013

Hibernate Mapping and Many to One example - part 1

Here is a multipart step by step tutorial style basic mapping. The diagram below depicts the basic database table structure.

Step 1: The ER diagram showing the relationships among the tables. The column names and types are omitted to keep it simple.



Step 2: The basic table details.


The UserApp table uses "UserName" and "AppCd" as the primary key. It links to the User table using the "UserName". "UserName" is the primary key in the User table.



The UserApp table links to the App table using the "AppCd".


"AppCd" is the primary key in the App table.


Step 3: Now the hibernate mapping part. Mapping UserApp table to UserApp object.

package com.myapp.model.security;

import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
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;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;


@SuppressWarnings("serial")
@Entity
@Table(name = "UserApp", schema = "dbo", catalog = "my_app")
public class UserApp implements java.io.Serializable
{
    
    private UserAppId id; // primary key
    private byte[] timestamp;
    private String groupNm;
    private char status;
    private Date createdOn;
    private String createdBy;
    private Date modifiedOn;
    private String modifiedBy;
    
 //linking tables and entities
    private CmnApp cmnApp;
    private CmnUser cmnUser;
    
 //default empty constructor
    public UserApp(){}
    
    public UserApp(UserAppId id, String groupNm, char status, Date createdOn, String createdBy, Date modifiedOn,
            String modifiedBy)
    {
        this.id = id;
        this.groupNm = groupNm;
        this.status = status;
        this.createdOn = createdOn;
        this.createdBy = createdBy;
        this.modifiedOn = modifiedOn;
        this.modifiedBy = modifiedBy;
    }
    
    public UserApp(UserAppId id, String groupNm, String previousGroupNm, char status, Date createdOn,
            String createdBy, Date modifiedOn, String modifiedBy)
    {
        this.id = id;
        this.groupNm = groupNm;
        this.previousGroupNm = previousGroupNm;
        this.status = status;
        this.createdOn = createdOn;
        this.createdBy = createdBy;
        this.modifiedOn = modifiedOn;
        this.modifiedBy = modifiedBy;
    }
    
    @EmbeddedId
    @AttributeOverrides(
    {
        @AttributeOverride(name = "userName", column = @Column(name = "UserName", nullable = false, length = 40)),
        @AttributeOverride(name = "appCd", column = @Column(name = "AppCd", nullable = false, length = 10))
    })
    public UserAppId getId()
    {
        return this.id;
    }
    
    public void setId(UserAppId id)
    {
        this.id = id;
    }
    
    @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 = "GroupNm", nullable = false, length = 40)
    public String getGroupNm()
    {
        return this.groupNm;
    }
    
    public void setGroupNm(String groupNm)
    {
        this.groupNm = groupNm;
    }
    
    @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;
    }
    
 
 //mapping links
 
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "AppCd", insertable = false, updatable = false)
    public CmnApp getCmnApp()
    {
        return cmnApp;
    }
    
    public void setCmnApp(CmnApp cmnApp)
    {
        this.cmnApp = cmnApp;
    }
    
    @ManyToOne(cascade = CascadeType.ALL, optional = true)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name = "UserName", insertable = false, updatable = false)
    public CmnUser getCmnUser()
    {
        return cmnUser;
    }
    
    public void setCmnUser(CmnUser cmnUser)
    {
        this.cmnUser = cmnUser;
    } 
}



Step 4: Define the primary key class UserAppId that maps the key columns.

package com.myapp.model.security;

import javax.persistence.Column;
import javax.persistence.Embeddable;


@SuppressWarnings("serial")
@Embeddable
public class UserAppId implements java.io.Serializable
{
    
    private String userName;
    private String appCd;
    
    public UserAppId()
    {
    }
    
    public UserAppId(String userName, String appCd)
    {
        this.userName = userName;
        this.appCd = appCd;
    }
    
    @Column(name = "UserName", nullable = false, length = 40)
    public String getUserName()
    {
        return this.userName;
    }
    
    public void setUserName(String userName)
    {
        this.userName = userName;
    }
    
    @Column(name = "AppCd", nullable = false, length = 10)
    public String getAppCd()
    {
        return this.appCd;
    }
    
    public void setAppCd(String appCd)
    {
        this.appCd = appCd;
    }
    
    public boolean equals(Object other)
    {
        if ((this == other))
            return true;
        if ((other == null))
            return false;
        if (!(other instanceof UserAppId))
            return false;
        UserAppId castOther = (UserAppId) other;
        
        return ((this.getUserName() == castOther.getUserName()) || (this.getUserName() != null
                && castOther.getUserName() != null && this.getUserName().equals(castOther.getUserName())))
                && ((this.getAppCd() == castOther.getAppCd()) || (this.getAppCd() != null
                        && castOther.getAppCd() != null && this.getAppCd().equals(castOther.getAppCd())));
    }
    
    public int hashCode()
    {
        int result = 17;
        
        result = 37 * result + (getUserName() == null ? 0 : this.getUserName().hashCode());
        result = 37 * result + (getAppCd() == null ? 0 : this.getAppCd().hashCode());
        return result;
    }  
}


The Hibernate Mapping and Many to One example - part 2 will cover the mapping of the other two tables.

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home