File: Professor.java
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Professor {
@Id
private int id;
private String name;
private long salary;
@Temporal(TemporalType.DATE)
private Date startDate;
@ManyToMany
private Collection projects;
public Professor() {
projects = new ArrayList();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setProjects(Collection projects) {
this.projects = projects;
}
public void setSalary(long salary) {
this.salary = salary;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public long getSalary() {
return salary;
}
public Date getStartDate() {
return startDate;
}
public Collection getProjects() {
return projects;
}
public String toString() {
return "Professor " + getId() +
": name: " + getName() +
", salary: " + getSalary();
}
}
File: ProfessorService.java
import java.util.Collection;
import javax.persistence.EntityManager;
public class ProfessorService {
protected EntityManager em;
public ProfessorService(EntityManager em) {
this.em = em;
}
public Professor createProfessor(String name, long salary) {
Professor emp = new Professor();
emp.setName(name);
emp.setSalary(salary);
em.persist(emp);
return emp;
}
public Project createProject(String name) {
Project space = new Project();
space.setName(name);
em.persist(space);
return space;
}
public Collection findAllProfessors() {
return (Collection) em.createQuery("SELECT e FROM Professor e").getResultList();
}
public void assignProfessorToProject(int empId, int projectId) {
Project project = em.find(Project.class, projectId);
Professor employee = em.find(Professor.class, empId);
project.getProfessors().add(employee);
employee.getProjects().add(project);
}
public Collection findAllProjects() {
return (Collection) em.createQuery("SELECT p FROM Project p").getResultList();
}
}
File: Project.java
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class Project {
@Id
protected int id;
protected String name;
@ManyToMany(mappedBy="projects")
private Collection employees;
public Project() {
employees = new ArrayList();
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public Collection getProfessors() {
return employees;
}
public void setProfessors(Collection employees) {
this.employees = employees;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "Project id: " + getId() + ", name: " + getName();
}
}
File: JPAUtil.java
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class JPAUtil {
Statement st;
public JPAUtil() throws Exception{
Class.forName("org.hsqldb.jdbcDriver");
System.out.println("Driver Loaded.");
String url = "jdbc:hsqldb:data/tutorial";
Connection conn = DriverManager.getConnection(url, "sa", "");
System.out.println("Got Connection.");
st = conn.createStatement();
}
public void executeSQLCommand(String sql) throws Exception {
st.executeUpdate(sql);
}
public void checkData(String sql) throws Exception {
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData metadata = rs.getMetaData();
for (int i = 0; i < metadata.getColumnCount(); i++) {
System.out.print("\t"+ metadata.getColumnLabel(i + 1));
}
System.out.println("\n----------------------------------");
while (rs.next()) {
for (int i = 0; i < metadata.getColumnCount(); i++) {
Object value = rs.getObject(i + 1);
if (value == null) {
System.out.print("\t ");
} else {
System.out.print("\t"+value.toString().trim());
}
}
System.out.println("");
}
}
}
File: Main.java
import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class Main {
public static void main(String[] a) throws Exception {
JPAUtil util = new JPAUtil();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfessorService");
EntityManager em = emf.createEntityManager();
ProfessorService service = new ProfessorService(em);
em.getTransaction().begin();
Professor emp = service.createProfessor("name",100);
Project proj = service.createProject("proj name");
service.assignProfessorToProject(emp.getId(),proj.getId());
for (Object o : service.findAllProfessors()) {
System.out.println(o );
}
for (Object o : service.findAllProjects()) {
System.out.println(o);
}
util.checkData("select * from Professor");
util.checkData("select * from Project");
util.checkData("select * from Professor_Project");
em.getTransaction().commit();
em.close();
emf.close();
}}
File: persistence.xml
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" version="1.0">
JPA-ManytoManyKeyTableIsCreatedAutomatically.zip( 5,337 k)