Last active
August 29, 2015 14:17
-
-
Save banterCZ/3a45bf50906053b0ecc5 to your computer and use it in GitHub Desktop.
JPA association class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
import java.util.Map; | |
@Entity | |
public class Employee { | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
@Id | |
@Column(name = "EMP_ID") | |
private Integer id; | |
@Column(name = "EMP_NAME") | |
private String name; | |
@ElementCollection | |
@CollectionTable(name = "EMP_PROJECTS", joinColumns = @JoinColumn(name = "EMP_ID")) | |
@MapKeyJoinColumn(name = "PROJECT_ID") | |
private Map<Project, ProjectAllocation> currentProjects; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE EMPLOYEE ( | |
EMP_ID INT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1), | |
EMP_NAME VARCHAR (100) NOT NULL, | |
PRIMARY KEY (EMP_ID) | |
); | |
CREATE TABLE PROJECT ( | |
PROJECT_ID INT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1), | |
PRIMARY KEY (PROJECT_ID) | |
); | |
CREATE TABLE EMP_PROJECTS ( | |
EMP_ID INT NOT NULL, | |
PROJECT_ID INT NOT NULL, | |
PROJECT_COMMENT VARCHAR (200) NOT NULL, | |
PROJECT_START_DATE DATE NOT NULL, | |
PROJECT_END_DATE DATE NOT NULL, | |
PROJECT_HOURS_PER_WEEK DOUBLE NOT NULL, | |
PRIMARY KEY (EMP_ID, PROJECT_ID) | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.*; | |
@Entity | |
public class Project { | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
@Id | |
@Column(name = "PROJECT_ID") | |
private Integer id; | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (!(o instanceof Project)) { | |
return false; | |
} | |
Project project = (Project) o; | |
if (id != null ? !id.equals(project.id) : project.id != null) { | |
return false; | |
} | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
return id != null ? id.hashCode() : 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.Column; | |
import javax.persistence.Embeddable; | |
import javax.persistence.Temporal; | |
import javax.persistence.TemporalType; | |
import java.util.Date; | |
@Embeddable | |
public class ProjectAllocation { | |
@Temporal(TemporalType.DATE) | |
@Column(name = "PROJECT_START_DATE") | |
private Date startDate; | |
@Temporal(TemporalType.DATE) | |
@Column(name = "PROJECT_END_DATE") | |
private Date endDate; | |
@Column(name = "PROJECT_COMMENT") | |
private String comment; | |
@Column(name = "PROJECT_HOURS_PER_WEEK") | |
private Double hoursPerWeek; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment