persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="persistenceUnit" >
<!-- transaction-type="RESOURCE_LOCAL" -->
<!-- <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> -->
<non-jta-data-source>jdbc/cms</non-jta-data-source>
<!-- shouldn't be valid for java SE per specification, but it works for EclipseLink ... -->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
main:
package domain;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.apache.commons.dbcp.BasicDataSource;
public class JpaTest {
private EntityManager manager;
public JpaTest(EntityManager manager) {
this.manager = manager;
}
/**
* @param args
* @throws NamingException
*/
public static void main(String[] args) throws NamingException {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
//System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
InitialContext context = new InitialContext();
BasicDataSource cmsDBDataSource = new BasicDataSource();
cmsDBDataSource.setDriverClassName("com.mysql.jdbc.Driver");
cmsDBDataSource.setUrl("jdbc:mysql://localhost:3306/jpa");
cmsDBDataSource.setUsername("root");
cmsDBDataSource.setPassword("password");
context.createSubcontext("jdbc");
context.bind("jdbc/cms", cmsDBDataSource);
EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
EntityManager manager = factory.createEntityManager();
JpaTest test = new JpaTest(manager);
EntityTransaction tx = manager.getTransaction();
tx.begin();
try {
test.createEmployees();
} catch (Exception e) {
e.printStackTrace();
}
tx.commit();
test.listEmployees();
Department d = null;
tx = manager.getTransaction();
tx.begin();
try {
d = test.deleteDepartment();
} catch (Exception e) {
e.printStackTrace();
}
tx.commit();
manager.clear();
if(d != null){
tx = manager.getTransaction();
tx.begin();
manager.persist(d);
tx.commit();
}
}
private void createEmployees() {
int numOfEmployees = manager.createQuery("Select a From Employee a").getResultList().size();
if (numOfEmployees == 0) {
Department department = new Department("java");
List<Employee> es = new ArrayList<Employee>();
es.add(new Employee("user 1",department));
es.add(new Employee("user 2",department));
department.getEmployees().addAll(es);
manager.persist(department);
es.get(0).setName("user 3");
}
}
@SuppressWarnings("unchecked")
private void listEmployees() {
List<Employee> resultList = manager.createQuery("Select a From Employee a").getResultList();
System.out.println("num of employess:" + resultList.size());
for (Employee next : resultList) {
System.out.println("next employee: " + next);
}
}
private Department deleteDepartment() {
List list = manager.createQuery("Select a From Department a")
.getResultList();
if(list.size() ==0){
return null;
}
Department department = (Department) list.get(0);
System.out.println("deleteDepartment: "+department.getName());
List<Employee> es = department.getEmployees();
if(es!=null){
for(Employee e:es){
System.out.println("Id:"+e.getId()+",E name: "+e.getName());
}
}
System.out.println(manager.contains(department));
//manager.remove(department);
return department;
}
}
entity1:
package domain;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Department {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
@OneToMany(mappedBy="department",cascade={CascadeType.PERSIST,
CascadeType.REMOVE
,CascadeType.MERGE
})
private List<Employee> employees = new ArrayList<Employee>();
public Department() {
super();
}
public Department(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
entity2:
package domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToOne
private Department department;
public Employee() {}
public Employee(String name, Department department) {
this.name = name;
this.department = department;
}
public Employee(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", department="
+ department.getName() + "]";
}
}
pom.xml
<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>
<groupId>standaloneJpa</groupId>
<artifactId>standaloneJpa</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.8.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>8.0.28</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbytools</artifactId>
<version>10.8.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/javax.persistence -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api -->
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
</dependencies>
</project>
http://www.nailedtothex.org/roller/kyle/entry/how-to-bind-lookup-a