Skip to content

Commit e87a1d7

Browse files
committed
Merge pull request javaee-samples#257 from arjantijms/master
Tests for the @OrderColumn annotation
2 parents 76b3e08 + a4bcb64 commit e87a1d7

File tree

15 files changed

+823
-0
lines changed

15 files changed

+823
-0
lines changed

jpa/ordercolumn/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.jpa</groupId>
6+
<artifactId>jpa-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<artifactId>ordercolumn</artifactId>
12+
<packaging>war</packaging>
13+
14+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.javaee7.jpa.ordercolumn.entity.bidirectionaljoin;
2+
3+
import static javax.persistence.GenerationType.IDENTITY;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.Id;
8+
import javax.persistence.JoinColumn;
9+
import javax.persistence.ManyToOne;
10+
11+
@Entity
12+
public class Child {
13+
14+
@Id
15+
@GeneratedValue(strategy = IDENTITY)
16+
private Long id;
17+
18+
@ManyToOne
19+
@JoinColumn(name = "parent_id", insertable = false, updatable = false)
20+
private Parent parent;
21+
22+
public Long getId() {
23+
return id;
24+
}
25+
26+
public void setId(Long id) {
27+
this.id = id;
28+
}
29+
30+
public Parent getParent() {
31+
return parent;
32+
}
33+
34+
public void setParent(Parent parent) {
35+
this.parent = parent;
36+
}
37+
38+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.javaee7.jpa.ordercolumn.entity.bidirectionaljoin;
2+
3+
import static javax.persistence.CascadeType.ALL;
4+
import static javax.persistence.FetchType.EAGER;
5+
import static javax.persistence.GenerationType.IDENTITY;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import javax.persistence.Entity;
11+
import javax.persistence.GeneratedValue;
12+
import javax.persistence.Id;
13+
import javax.persistence.JoinColumn;
14+
import javax.persistence.OneToMany;
15+
import javax.persistence.OrderColumn;
16+
17+
@Entity
18+
public class Parent {
19+
20+
@Id
21+
@GeneratedValue(strategy = IDENTITY)
22+
private Long id;
23+
24+
@OneToMany(cascade = ALL, fetch = EAGER)
25+
@OrderColumn
26+
@JoinColumn(name = "parent_id")
27+
private List<Child> children = new ArrayList<>();
28+
29+
public Long getId() {
30+
return id;
31+
}
32+
33+
public void setId(Long id) {
34+
this.id = id;
35+
}
36+
37+
public List<Child> getChildren() {
38+
return children;
39+
}
40+
41+
public void setChildren(List<Child> children) {
42+
this.children = children;
43+
}
44+
45+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.javaee7.jpa.ordercolumn.entity.bidirectionalmappedby;
2+
3+
import static javax.persistence.GenerationType.IDENTITY;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.Id;
8+
import javax.persistence.ManyToOne;
9+
10+
@Entity
11+
public class Child {
12+
13+
@Id
14+
@GeneratedValue(strategy = IDENTITY)
15+
private Long id;
16+
17+
@ManyToOne
18+
private Parent parent;
19+
20+
public Long getId() {
21+
return id;
22+
}
23+
24+
public void setId(Long id) {
25+
this.id = id;
26+
}
27+
28+
public Parent getParent() {
29+
return parent;
30+
}
31+
32+
public void setParent(Parent parent) {
33+
this.parent = parent;
34+
}
35+
36+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.javaee7.jpa.ordercolumn.entity.bidirectionalmappedby;
2+
3+
import static javax.persistence.CascadeType.ALL;
4+
import static javax.persistence.FetchType.EAGER;
5+
import static javax.persistence.GenerationType.IDENTITY;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import javax.persistence.Entity;
11+
import javax.persistence.GeneratedValue;
12+
import javax.persistence.Id;
13+
import javax.persistence.OneToMany;
14+
import javax.persistence.OrderColumn;
15+
16+
@Entity
17+
public class Parent {
18+
19+
@Id
20+
@GeneratedValue(strategy = IDENTITY)
21+
private Long id;
22+
23+
@OneToMany(cascade = ALL, fetch = EAGER, mappedBy = "parent")
24+
@OrderColumn
25+
private List<Child> children = new ArrayList<>();
26+
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
public void setId(Long id) {
32+
this.id = id;
33+
}
34+
35+
public List<Child> getChildren() {
36+
return children;
37+
}
38+
39+
public void setChildren(List<Child> children) {
40+
this.children = children;
41+
}
42+
43+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.javaee7.jpa.ordercolumn.entity.unidirectional;
2+
3+
import static javax.persistence.GenerationType.IDENTITY;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class Child {
11+
12+
@Id
13+
@GeneratedValue(strategy = IDENTITY)
14+
private Long id;
15+
16+
public Long getId() {
17+
return id;
18+
}
19+
20+
public void setId(Long id) {
21+
this.id = id;
22+
}
23+
24+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.javaee7.jpa.ordercolumn.entity.unidirectional;
2+
3+
import static javax.persistence.CascadeType.ALL;
4+
import static javax.persistence.FetchType.EAGER;
5+
import static javax.persistence.GenerationType.IDENTITY;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import javax.persistence.Entity;
11+
import javax.persistence.GeneratedValue;
12+
import javax.persistence.Id;
13+
import javax.persistence.JoinColumn;
14+
import javax.persistence.OneToMany;
15+
import javax.persistence.OrderColumn;
16+
17+
@Entity
18+
public class Parent {
19+
20+
@Id
21+
@GeneratedValue(strategy = IDENTITY)
22+
private Long id;
23+
24+
@OneToMany(cascade = ALL, fetch = EAGER)
25+
@OrderColumn
26+
@JoinColumn
27+
private List<Child> children = new ArrayList<>();
28+
29+
public Long getId() {
30+
return id;
31+
}
32+
33+
public void setId(Long id) {
34+
this.id = id;
35+
}
36+
37+
public List<Child> getChildren() {
38+
return children;
39+
}
40+
41+
public void setChildren(List<Child> children) {
42+
this.children = children;
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.javaee7.jpa.ordercolumn.service.bidirectionaljoin;
2+
3+
import javax.ejb.Stateless;
4+
import javax.persistence.EntityManager;
5+
import javax.persistence.PersistenceContext;
6+
7+
import org.javaee7.jpa.ordercolumn.entity.bidirectionaljoin.Parent;
8+
9+
@Stateless
10+
public class OrderColumnTesterService {
11+
12+
@PersistenceContext
13+
EntityManager entityManager;
14+
15+
public Parent save(Parent parent) {
16+
return entityManager.merge(parent);
17+
}
18+
19+
public Parent getParentById(Long id) {
20+
return entityManager.find(Parent.class, id);
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.javaee7.jpa.ordercolumn.service.bidirectionalmappedby;
2+
3+
import javax.ejb.Stateless;
4+
import javax.persistence.EntityManager;
5+
import javax.persistence.PersistenceContext;
6+
7+
import org.javaee7.jpa.ordercolumn.entity.bidirectionalmappedby.Parent;
8+
9+
@Stateless
10+
public class OrderColumnTesterService {
11+
12+
@PersistenceContext
13+
EntityManager entityManager;
14+
15+
public Parent save(Parent parent) {
16+
return entityManager.merge(parent);
17+
}
18+
19+
public Parent getParentById(Long id) {
20+
return entityManager.find(Parent.class, id);
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.javaee7.jpa.ordercolumn.service.unidirectional;
2+
3+
import javax.ejb.Stateless;
4+
import javax.persistence.EntityManager;
5+
import javax.persistence.PersistenceContext;
6+
7+
import org.javaee7.jpa.ordercolumn.entity.unidirectional.Parent;
8+
9+
@Stateless
10+
public class OrderColumnTesterService {
11+
12+
@PersistenceContext
13+
EntityManager entityManager;
14+
15+
public Parent save(Parent parent) {
16+
return entityManager.merge(parent);
17+
}
18+
19+
public Parent getParentById(Long id) {
20+
return entityManager.find(Parent.class, id);
21+
}
22+
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
4+
5+
<persistence-unit name="testPU">
6+
<properties>
7+
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
8+
</properties>
9+
</persistence-unit>
10+
</persistence>

0 commit comments

Comments
 (0)