Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions jpa/ordercolumn/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.javaee7.jpa</groupId>
<artifactId>jpa-samples</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>ordercolumn</artifactId>
<packaging>war</packaging>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.javaee7.jpa.ordercolumn.entity.bidirectionaljoin;

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Child {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "parent_id", insertable = false, updatable = false)
private Parent parent;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Parent getParent() {
return parent;
}

public void setParent(Parent parent) {
this.parent = parent;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.javaee7.jpa.ordercolumn.entity.bidirectionaljoin;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.FetchType.EAGER;
import static javax.persistence.GenerationType.IDENTITY;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;

@Entity
public class Parent {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

@OneToMany(cascade = ALL, fetch = EAGER)
@OrderColumn
@JoinColumn(name = "parent_id")
private List<Child> children = new ArrayList<>();

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public List<Child> getChildren() {
return children;
}

public void setChildren(List<Child> children) {
this.children = children;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.javaee7.jpa.ordercolumn.entity.bidirectionalmappedby;

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

@Entity
public class Child {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

@ManyToOne
private Parent parent;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Parent getParent() {
return parent;
}

public void setParent(Parent parent) {
this.parent = parent;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.javaee7.jpa.ordercolumn.entity.bidirectionalmappedby;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.FetchType.EAGER;
import static javax.persistence.GenerationType.IDENTITY;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;

@Entity
public class Parent {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

@OneToMany(cascade = ALL, fetch = EAGER, mappedBy = "parent")
@OrderColumn
private List<Child> children = new ArrayList<>();

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public List<Child> getChildren() {
return children;
}

public void setChildren(List<Child> children) {
this.children = children;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.javaee7.jpa.ordercolumn.entity.unidirectional;

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Child {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.javaee7.jpa.ordercolumn.entity.unidirectional;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.FetchType.EAGER;
import static javax.persistence.GenerationType.IDENTITY;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;

@Entity
public class Parent {

@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

@OneToMany(cascade = ALL, fetch = EAGER)
@OrderColumn
@JoinColumn
private List<Child> children = new ArrayList<>();

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public List<Child> getChildren() {
return children;
}

public void setChildren(List<Child> children) {
this.children = children;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.javaee7.jpa.ordercolumn.service.bidirectionaljoin;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.javaee7.jpa.ordercolumn.entity.bidirectionaljoin.Parent;

@Stateless
public class OrderColumnTesterService {

@PersistenceContext
EntityManager entityManager;

public Parent save(Parent parent) {
return entityManager.merge(parent);
}

public Parent getParentById(Long id) {
return entityManager.find(Parent.class, id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.javaee7.jpa.ordercolumn.service.bidirectionalmappedby;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.javaee7.jpa.ordercolumn.entity.bidirectionalmappedby.Parent;

@Stateless
public class OrderColumnTesterService {

@PersistenceContext
EntityManager entityManager;

public Parent save(Parent parent) {
return entityManager.merge(parent);
}

public Parent getParentById(Long id) {
return entityManager.find(Parent.class, id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.javaee7.jpa.ordercolumn.service.unidirectional;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.javaee7.jpa.ordercolumn.entity.unidirectional.Parent;

@Stateless
public class OrderColumnTesterService {

@PersistenceContext
EntityManager entityManager;

public Parent save(Parent parent) {
return entityManager.merge(parent);
}

public Parent getParentById(Long id) {
return entityManager.find(Parent.class, id);
}

}
10 changes: 10 additions & 0 deletions jpa/ordercolumn/src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

<persistence-unit name="testPU">
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create" />
</properties>
</persistence-unit>
</persistence>
Loading