Skip to content

Instantly share code, notes, and snippets.

@TurekBot
Forked from james-d/Investigator.java
Created March 8, 2018 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TurekBot/780855bf33e2f3ebe694eb0e39dd0a75 to your computer and use it in GitHub Desktop.
Save TurekBot/780855bf33e2f3ebe694eb0e39dd0a75 to your computer and use it in GitHub Desktop.
Example of JPA entities that use JavaFX properties. These use a "super-lazy" idiom for instantiating the properties, and implement Externalizable to work around the lack of Serialization support in the FX property classes.
package edu.marshall.genomics.lims.entities;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
* Entity implementation class for Entity: Investigator
*
*/
@Entity
@Access(AccessType.PROPERTY)
@Table(name="investigators")
@NamedQuery(name="Investigator.findAll", query="SELECT i from Investigator i")
public class Investigator implements Externalizable {
private static final long serialVersionUID = 1L;
private IntegerProperty id ;
private int _id ;
@Id
@Column(name="inv_id")
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
if (id==null) {
return _id ;
} else {
return id.get();
}
}
public void setId(int id) {
if (this.id==null) {
_id=id ;
} else {
this.id.set(id);
}
}
public IntegerProperty idProperty() {
if (id==null) {
id = new SimpleIntegerProperty(this, "id", _id);
}
return id ;
}
private StringProperty email ;
private String _email ;
@Column(name="email")
public String getEmail() {
if (email == null) {
return _email ;
} else {
return email.get() ;
}
}
public void setEmail(String email) {
if (this.email == null) {
_email = email ;
} else {
this.email.set(email);
}
}
public StringProperty emailProperty() {
if (email == null) {
email = new SimpleStringProperty(this, "email", _email);
}
return email ;
}
private StringProperty firstName ;
private String _firstName ;
@Column(name="firstName")
public String getFirstName() {
if (firstName == null) {
return _firstName ;
} else {
return firstName.get();
}
}
public void setFirstName(String firstName) {
if (this.firstName == null) {
_firstName = firstName ;
} else {
this.firstName.set(firstName);
}
}
public StringProperty firstNameProperty() {
if (firstName == null) {
firstName = new SimpleStringProperty(this, "firstName", _firstName);
}
return firstName ;
}
private StringProperty lastName ;
private String _lastName ;
@Column(name="lastName")
public String getLastName() {
if (lastName == null) {
return _lastName ;
} else {
return lastName.get();
}
}
public void setLastName(String lastName) {
if (this.lastName == null) {
_lastName = lastName ;
} else {
this.lastName.set(lastName);
}
}
public StringProperty lastNameProperty() {
if (lastName == null) {
this.lastName = new SimpleStringProperty(this, "lastName", _lastName);
}
return lastName ;
}
private StringProperty institution ;
private String _institution ;
@Column(name="institution")
public String getInstitution() {
if (institution == null) {
return _institution ;
} else {
return institution.get();
}
}
public void setInstitution(String institution) {
if (this.institution == null) {
_institution = institution ;
} else {
this.institution.set(institution);
}
}
public StringProperty institutionProperty() {
if (institution == null) {
institution = new SimpleStringProperty(this, "institution", _institution);
}
return institution ;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(getId());
out.writeObject(getFirstName());
out.writeObject(getLastName());
out.writeObject(getEmail());
out.writeObject(getInstitution());
}
@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
setId(in.readInt());
setFirstName((String) in.readObject());
setLastName((String) in.readObject());
setEmail((String) in.readObject());
setInstitution((String) in.readObject());
}
}
package edu.marshall.genomics.lims.entities;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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;
/**
* Entity implementation class for Entity: Project
*
*/
@Entity
@Access(AccessType.PROPERTY)
@Table(name="projects")
@NamedQueries(
{
@NamedQuery(name="Project.findAll", query="SELECT p from Project p"),
@NamedQuery(name="Project.byInvestigator", query="SELECT p FROM Project p WHERE p.investigator=:investigator"),
}
)
public class Project implements Externalizable {
private static final long serialVersionUID = 1L;
private IntegerProperty id;
private int _id;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "project_id")
public final int getId() {
if (id == null) {
return _id;
} else {
return id.get();
}
}
public final void setId(int id) {
if (this.id == null) {
_id = id;
} else {
this.id.set(id);
}
}
public IntegerProperty idProperty() {
if (id == null) {
id = new SimpleIntegerProperty(this, "id", _id);
}
return id ;
}
private StringProperty title ;
private String _title ;
@Column(name="title")
public final String getTitle() {
if (this.title == null) {
return _title ;
} else {
return title.get();
}
}
public final void setTitle(String title) {
if (this.title == null) {
_title = title ;
} else {
this.title.set(title);
}
}
public StringProperty titleProperty() {
if (title == null) {
title = new SimpleStringProperty(this, "title", _title);
}
return title ;
}
private ObjectProperty<Investigator> investigator ;
private Investigator _investigator ;
@ManyToOne(cascade=CascadeType.MERGE, fetch=FetchType.EAGER)
@JoinColumn(name="investigator", referencedColumnName="inv_id")
public final Investigator getInvestigator() {
if (investigator == null) {
return _investigator ;
} else {
return investigator.get();
}
}
public final void setInvestigator(Investigator investigator) {
if (this.investigator == null) {
_investigator = investigator ;
} else {
this.investigator.set(investigator);
}
}
public ObjectProperty<Investigator> investigatorProperty() {
if (investigator == null) {
investigator = new SimpleObjectProperty<>(this, "investigator", _investigator);
}
return investigator ;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(getId());
out.writeObject(getTitle());
out.writeObject(getInvestigator());
}
@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
setId(in.readInt());
setTitle((String)in.readObject());
setInvestigator((Investigator) in.readObject());
}
}
package edu.marshall.genomics.lims.entities;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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;
/**
* Entity implementation class for Entity: samples
*
*/
@Entity
@Access(AccessType.PROPERTY)
@Table(name = "samples")
@NamedQueries({
@NamedQuery(name = "Sample.findAll", query = "SELECT s FROM Sample s"),
@NamedQuery(name = "Sample.byProject", query = "SELECT s FROM Sample s where s.project=:project") })
public class Sample implements Externalizable {
private static final long serialVersionUID = 1L;
private IntegerProperty id;
private int _id;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public final int getId() {
if (this.id == null) {
return _id;
} else {
return id.get();
}
}
public final void setId(int id) {
if (this.id == null) {
_id = id;
} else {
this.id.set(id);
}
}
public IntegerProperty idProperty() {
if (id == null) {
id = new SimpleIntegerProperty(this, "id", _id);
}
return id;
}
private StringProperty sampleId;
private String _sampleId;
@Column(name = "sampleId")
public final String getSampleId() {
if (sampleId == null) {
return _sampleId;
} else {
return sampleId.get();
}
}
public final void setSampleId(String sampleId) {
if (this.sampleId == null) {
_sampleId = sampleId;
} else {
this.sampleId.set(sampleId);
}
}
public StringProperty sampleIdProperty() {
if (sampleId == null) {
sampleId = new SimpleStringProperty(this, "sampleId", _sampleId);
}
return sampleId;
}
private ObjectProperty<Project> project;
private Project _project;
@JoinColumn(name = "project", referencedColumnName = "project_id")
@ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
public final Project getProject() {
if (project == null) {
return _project;
} else {
return project.get();
}
}
public final void setProject(Project project) {
if (this.project == null) {
_project = project;
} else {
this.project.set(project);
}
}
public ObjectProperty<Project> projectProperty() {
if (project == null) {
project = new SimpleObjectProperty<>(this, "project", _project);
}
return project;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(getId());
out.writeObject(getSampleId());
out.writeObject(getProject());
}
@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
setId(in.readInt());
setSampleId((String) in.readObject());
setProject((Project) in.readObject());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment