Skip to content

Commit c3a6687

Browse files
committed
Added sample for Entity Graph.
1 parent 8d80dbd commit c3a6687

File tree

14 files changed

+382
-353
lines changed

14 files changed

+382
-353
lines changed

jpa/entitygraph/pom.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

11-
<groupId>org.javaee7.jpa</groupId>
1211
<artifactId>entitygraph</artifactId>
13-
<version>1.0-SNAPSHOT</version>
1412
<packaging>war</packaging>
1513
</project>
Lines changed: 48 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,78 @@
11
package org.javaee7.jpa.entitygraph;
22

3-
import java.io.Serializable;
4-
import javax.persistence.CascadeType;
5-
import javax.persistence.Column;
6-
import javax.persistence.Entity;
7-
import javax.persistence.FetchType;
8-
import javax.persistence.Id;
9-
import javax.persistence.NamedAttributeNode;
10-
import javax.persistence.NamedEntityGraph;
11-
import javax.persistence.NamedQueries;
12-
import javax.persistence.NamedQuery;
13-
import javax.persistence.OneToOne;
14-
import javax.persistence.Table;
3+
import javax.persistence.*;
154
import javax.validation.constraints.NotNull;
165
import javax.validation.constraints.Size;
17-
import javax.xml.bind.annotation.XmlRootElement;
6+
import java.io.Serializable;
7+
import java.util.List;
8+
import java.util.Set;
189

1910
/**
2011
* @author Arun Gupta
2112
*/
2213
@Entity
2314
@Table(name = "MOVIE_ENTITY_GRAPH")
24-
@XmlRootElement
2515
@NamedQueries({
26-
@NamedQuery(name = "Movie.findAll", query = "SELECT m FROM Movie m"),
27-
@NamedQuery(name = "Movie.findById", query = "SELECT m FROM Movie m WHERE m.id = :id")})
28-
@NamedEntityGraph(attributeNodes = { @NamedAttributeNode("name") })
16+
@NamedQuery(name = "Movie.findAll", query = "SELECT m FROM Movie m")
17+
})
18+
@NamedEntityGraphs({
19+
@NamedEntityGraph(
20+
name = "movieWithActors",
21+
attributeNodes = {
22+
@NamedAttributeNode("movieActors")
23+
}
24+
),
25+
@NamedEntityGraph(
26+
name = "movieWithActorsAndAwards",
27+
attributeNodes = {
28+
@NamedAttributeNode(value = "movieActors", subgraph = "movieActorsGraph")
29+
},
30+
subgraphs = {
31+
@NamedSubgraph(
32+
name = "movieActorsGraph",
33+
attributeNodes = {
34+
@NamedAttributeNode("movieActorAwards")
35+
}
36+
)
37+
}
38+
)
39+
})
2940
public class Movie implements Serializable {
30-
31-
private static final long serialVersionUID = 1L;
3241
@Id
33-
@NotNull
34-
@Column(name = "ID")
3542
private Integer id;
36-
43+
44+
@NotNull
3745
@Size(max = 50)
38-
@Column(name = "NAME")
3946
private String name;
40-
41-
@OneToOne(cascade = CascadeType.ALL, mappedBy = "movieEntityGraph")
42-
// @OneToOne(cascade = CascadeType.ALL, mappedBy = "movieEntityGraph", fetch = FetchType.LAZY)
43-
private MovieActors movieActors;
44-
45-
public Movie() {
46-
}
47-
48-
public Movie(Integer id) {
49-
this.id = id;
50-
}
51-
52-
public Integer getId() {
53-
return id;
54-
}
5547

56-
public void setId(Integer id) {
57-
this.id = id;
58-
}
48+
@OneToMany
49+
@JoinColumn(name = "ID")
50+
private List<MovieActor> movieActors;
5951

60-
public String getName() {
61-
return name;
62-
}
52+
@OneToMany(fetch = FetchType.EAGER)
53+
@JoinColumn(name = "ID")
54+
private List<MovieDirector> movieDirectors;
6355

64-
public void setName(String name) {
65-
this.name = name;
66-
}
56+
@OneToMany
57+
@JoinColumn(name = "ID")
58+
private List<MovieAward> movieAwards;
6759

68-
public MovieActors getMovieActors() {
60+
public List<MovieActor> getMovieActors() {
6961
return movieActors;
7062
}
7163

72-
public void setMovieActors(MovieActors movieActors) {
73-
this.movieActors = movieActors;
74-
}
75-
7664
@Override
77-
public int hashCode() {
78-
int hash = 0;
79-
hash += (id != null ? id.hashCode() : 0);
80-
return hash;
81-
}
65+
public boolean equals(Object o) {
66+
if (this == o) return true;
67+
if (o == null || getClass() != o.getClass()) return false;
8268

83-
@Override
84-
public boolean equals(Object object) {
85-
// TODO: Warning - this method won't work in the case the id fields are not set
86-
if (!(object instanceof Movie)) {
87-
return false;
88-
}
89-
Movie other = (Movie) object;
90-
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
91-
return false;
92-
}
93-
return true;
69+
Movie movie = (Movie) o;
70+
71+
return id.equals(movie.id);
9472
}
9573

9674
@Override
97-
public String toString() {
98-
return "org.glassfish.enttygraph.MovieEntityGraph[ id=" + id + " ]";
75+
public int hashCode() {
76+
return id.hashCode();
9977
}
10078
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.javaee7.jpa.entitygraph;
2+
3+
import javax.persistence.*;
4+
import javax.validation.constraints.NotNull;
5+
import javax.validation.constraints.Size;
6+
import java.io.Serializable;
7+
import java.util.Set;
8+
9+
/**
10+
* @author Arun Gupta
11+
*/
12+
@Entity
13+
@Table(name = "MOVIE_ACTORS_ENTITY_GRAPH")
14+
public class MovieActor implements Serializable {
15+
@Id
16+
private Integer id;
17+
18+
@NotNull
19+
@Size(max = 50)
20+
private String actor;
21+
22+
@OneToMany
23+
@JoinColumn(name = "ID")
24+
private Set<MovieActorAward> movieActorAwards;
25+
26+
@Override
27+
public boolean equals(Object o) {
28+
if (this == o) { return true; }
29+
if (o == null || getClass() != o.getClass()) { return false; }
30+
31+
MovieActor that = (MovieActor) o;
32+
33+
return id.equals(that.id);
34+
}
35+
36+
@Override
37+
public int hashCode() {
38+
return id.hashCode();
39+
}
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.javaee7.jpa.entitygraph;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
import javax.persistence.Table;
6+
import javax.validation.constraints.NotNull;
7+
import javax.validation.constraints.Size;
8+
9+
/**
10+
* @author Roberto Cortez
11+
*/
12+
@Entity
13+
@Table(name = "MOVIE_ACTOR_AWARDS_ENTITY_GRAPH")
14+
public class MovieActorAward {
15+
@Id
16+
private Integer id;
17+
18+
@NotNull
19+
@Size(min = 1, max = 50)
20+
private String award;
21+
22+
@Override
23+
public boolean equals(Object o) {
24+
if (this == o) return true;
25+
if (o == null || getClass() != o.getClass()) return false;
26+
27+
MovieActorAward that = (MovieActorAward) o;
28+
29+
return id.equals(that.id);
30+
}
31+
32+
@Override
33+
public int hashCode() {
34+
return id.hashCode();
35+
}
36+
}

jpa/entitygraph/src/main/java/org/javaee7/jpa/entitygraph/MovieActors.java

Lines changed: 0 additions & 119 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.javaee7.jpa.entitygraph;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
import javax.persistence.Table;
6+
import javax.validation.constraints.NotNull;
7+
import javax.validation.constraints.Size;
8+
import java.io.Serializable;
9+
10+
/**
11+
* @author Roberto Cortez
12+
*/
13+
@Entity
14+
@Table(name = "MOVIE_AWARDS_ENTITY_GRAPH")
15+
public class MovieAward implements Serializable {
16+
@Id
17+
private Integer id;
18+
19+
@NotNull
20+
@Size(min = 1, max = 50)
21+
private String award;
22+
23+
@Override
24+
public boolean equals(Object o) {
25+
if (this == o) return true;
26+
if (o == null || getClass() != o.getClass()) return false;
27+
28+
MovieAward that = (MovieAward) o;
29+
30+
return id.equals(that.id);
31+
}
32+
33+
@Override
34+
public int hashCode() {
35+
return id.hashCode();
36+
}
37+
}

0 commit comments

Comments
 (0)