Spring JPA Embeddable Id example

Embeddable Entity

package com.sforce.model;

import java.io.Serializable;
import javax.persistence.*;

@Embeddable
public class MonthlyPK implements Serializable {
	
	private static final long serialVersionUID = 1L;

	@Column(name="AID")
	private long aid;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="N_DATE")
	private java.util.Date nDate;

	public MonthlyPK() {
	}
	public long getAid() {
		return this.aid;
	}
	public void setAid(long aid) {
		this.aid = aid;
	}
	public java.util.Date getNDate() {
		return this.nDate;
	}
	public void setNDate(java.util.Date nDate) {
		this.nDate = nDate;
	}

	public boolean equals(Object other) {
		if (this == other) {
			return true;
		}
		if (!(other instanceof MonthlyPK)) {
			return false;
		}
		MonthlyPK castOther = (MonthlyPK)other;
		return 
			(this.aid == castOther.aid)
			&& this.nDate.equals(castOther.nDate);
	}

	public int hashCode() {
		final int prime = 31;
		int hash = 17;
		hash = hash * prime + ((int) (this.aid ^ (this.aid >>> 32)));
		hash = hash * prime + this.nDate.hashCode();
		
		return hash;
	}
}

Embeddable Id as @Id

package com.sforce.model;

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 java.io.Serializable;

import java.math.BigDecimal;
import java.util.Date;

@Entity 
@Table(name="DEV$MONTHLY")
public class DevMonthly implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	private MonthlyNarPK monthlyNarPK;
	
	@Column(name="AID", insertable=false, updatable=false)
	private Long id;

	@Column(name="CHNG_STATUS")
	private BigDecimal chngStatus;

	@Column(name="NAR")
	private BigDecimal nar;

	@Column(name="SF_AMID")
	private String sfAmid;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="NAR_DATE", insertable=false, updatable=false)
	private Date narDate;

	public DevMonthly() {}
	
	public DevMonthly(String sfAmid){
		this.sfAmid = sfAmid;
	}
	
	public DevMonthly(Long id, String sfAmid, Date narDate) {
		this.id = id;
		this.sfAmid = sfAmid;
		this.narDate = narDate;
	}

	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public BigDecimal getChngStatus() {
		return this.chngStatus;
	}

	public void setChngStatus(BigDecimal chngStatus) {
		this.chngStatus = chngStatus;
	}

	public BigDecimal getNar() {
		return this.nar;
	}

	public void setNar(BigDecimal nar) {
		this.nar = nar;
	}

	public String getSfAmid() {
		return this.sfAmid;
	}

	public void setSfAmid(String sfAmid) {
		this.sfAmid = sfAmid;
	}

	public Date getNarDate() {
		return narDate;
	}

	public void setNarDate(Date narDate) {
		this.narDate = narDate;
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s