Database:
CREATE TABLE `encountermessage` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`encounterID` bigint(20) NOT NULL,
`message` varchar(500) NOT NULL,
`version` bigint(20) DEFAULT '1',
`createdBy` bigint(20) NOT NULL,
`createdDate` datetime NOT NULL,
`updatedDate` datetime DEFAULT NULL,
`updatedBy` bigint(20) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `FK_encountermessage_encounter` (`encounterID`),
KEY `FK_encountermessage_createdby` (`createdBy`),
CONSTRAINT `FK_encountermessage_createdby` FOREIGN KEY (`createdBy`) REFERENCES `users` (`ID`),
CONSTRAINT `FK_encountermessage_encounter` FOREIGN KEY (`encounterID`) REFERENCES `encounter` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `encountermessageusr` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`messageID` bigint(20) NOT NULL,
`userID` bigint(20) NOT NULL,
`readDate` datetime NOT NULL,
PRIMARY KEY (`ID`),
KEY `encountermessageusr_messageID` (`messageID`),
KEY `encountermessageusr_userID` (`userID`),
CONSTRAINT `encountermessageusr_messageID` FOREIGN KEY (`messageID`) REFERENCES `encountermessage` (`ID`),
CONSTRAINT `encountermessageusr_userID` FOREIGN KEY (`userID`) REFERENCES `users` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Hibernate Annotation Model Classes
Base.java
import java.io.Serializable;
import java.util.Date;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
@MappedSuperclass
public abstract class Base implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long ID;
@Version
private Long version;
private Long createdBy;
@Temporal(value = TemporalType.TIMESTAMP)
private Date createdDate;
private Long updatedBy;
@Temporal(value = TemporalType.TIMESTAMP)
private Date updatedDate;
public long getID() {
return ID;
}
public void setID(long iD) {
ID = iD;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public Long getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Long createdBy) {
this.createdBy = createdBy;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public Long getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(Long updatedBy) {
this.updatedBy = updatedBy;
}
public Date getUpdatedDate() {
return updatedDate;
}
public void setUpdatedDate(Date updatedDate) {
this.updatedDate = updatedDate;
}
}
EncounterMessage.java
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.validator.constraints.NotBlank;
@Entity
@Table(name = "encountermessage")
@NamedQueries({ @NamedQuery(name = "EncounterMessage.getAllRecords", query = "Select em from EncounterMessage as em order by createdDate") })
public class EncounterMessage extends Base implements Serializable {
private static final long serialVersionUID = 1L;
public EncounterMessage() {
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@OneToMany(mappedBy = "encounterMessage")
private Set<EncounterMessageUsr> encounterMessageUsrs;
@NotBlank(message = "EncounterID cannot be empty")
private Long encounterID;
@NotBlank(message = "Message cannot be empty")
private String message;
public Set<EncounterMessageUsr> getEncounterMessageUsrs() {
return encounterMessageUsrs;
}
public void setEncounterMessageUsrs(Set<EncounterMessageUsr> encounterMessageUsrs) {
this.encounterMessageUsrs = encounterMessageUsrs;
}
public Long getEncounterID() {
return encounterID;
}
public void setEncounterID(Long encounterID) {
this.encounterID = encounterID;
}
}
EncounterMessageUsr.java
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "encountermessageusr")
@NamedQueries({ @NamedQuery(name = "EncounterMessageUsr.getAllRecords", query = "Select em from EncounterMessageUsr as em order by raedDate") })
public class EncounterMessageUsr implements Serializable {
private static final long serialVersionUID = 1L;
public EncounterMessageUsr() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long ID;
private Long userID;
@Temporal(value = TemporalType.TIMESTAMP)
private Date readDate;
@ManyToOne
@JoinColumn(name = "messageID", nullable = false)
private EncounterMessage encounterMessage;
public Long getUserID() {
return userID;
}
public void setUserID(Long userID) {
this.userID = userID;
}
public EncounterMessage getEncounterMessage() {
return encounterMessage;
}
public void setEncounterMessage(EncounterMessage encounterMessage) {
this.encounterMessage = encounterMessage;
}
public Date getReadDate() {
return readDate;
}
public void setReadDate(Date readDate) {
this.readDate = readDate;
}
}