Java Tutorial/JPA/Cascade Action — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:18, 1 июня 2010
Cascade Type: REFRESH
File: Employee.java
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
@Entity
public class Employee {
@Id
private int id;
private String name;
@ManyToOne(cascade = { CascadeType.REFRESH })
private Employee manager;
@OneToMany(mappedBy = "manager")
private Collection<Employee> directs = new ArrayList<Employee>();
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 Collection<Employee> getDirects() {
return directs;
}
public void addDirect(Employee employee) {
if (!getDirects().contains(employee)) {
getDirects().add(employee);
if (employee.getManager() != null) {
employee.getManager().getDirects().remove(employee);
}
employee.setManager(this);
}
}
public Employee getManager() {
return manager;
}
public void setManager(Employee manager) {
this.manager = manager;
}
}
With Cascade.All Linked Entity Auto Save
File: Helper.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class Helper {
public static void checkData() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial", "sa", "");
Statement st = conn.createStatement();
ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
while (mrs.next()) {
String tableName = mrs.getString(3);
System.out.println("\n\n\n\nTable Name: "+ tableName);
ResultSet rs = st.executeQuery("select * from " + tableName);
ResultSetMetaData metadata = rs.getMetaData();
while (rs.next()) {
System.out.println(" Row:");
for (int i = 0; i < metadata.getColumnCount(); i++) {
System.out.println(" Column Name: "+ metadata.getColumnLabel(i + 1)+ ", ");
System.out.println(" Column Type: "+ metadata.getColumnTypeName(i + 1)+ ": ");
Object value = rs.getObject(i + 1);
System.out.println(" Column Value: "+value+"\n");
}
}
}
}
}