Hi,
in my application I need a data structure to make it possible save parent child relations. My special case here is that each child can also be a parent and so on.
Therefore there is a abstract class used as superclass for all entities
Code:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class BaseCategory<T extends StandardEntity> extends StandardEntity {
private Long id;
private List<T> childs;
private T parent;
@Id @GeneratedValue(strategy=GenerationType.TABLE)
public Long getId(){return id;}
public void setId(Long id){this.id = id;}
@OneToMany(cascade=CascadeType.ALL, targetEntity=BaseCategory.class, mappedBy="parent")
public List<T> getChilds() {return childs;}
public void setChilds(List<T> childs) {this.childs = childs;}
@ManyToOne()
public T getParent() {return parent;}
public void setParent(T parent) {this.parent = parent;}
}
And I have many entity classes using the BaseCategory above. Here one example, the others created in the same way.
Code:
@Entity
@Name("package")
public class Package extends BaseCategory<BaseCategory> implements Serializable {
private static final long serialVersionUID = 5330799040159648995L;
private Long package_id;
private String package_name;
private Long package_totalCount = new Long(0);
public Long getPackage_id() {return package_id;}
public void setPackage_id(Long package_id) {this.ackage_id = ackage_id;}
public String getPackage_name() {return package_name;}
public void setPackage_name(String package_name) {this.package_name = package_name;}
public Long getPackage_totalCount() {return package_totalCount;}
public void setPackage_totalCount(Long package_totalCount) {this.package_totalCount = package_totalCount;}
}
in my business code i use:
Code:
records = new ArrayList<BaseCategory>();
package= new Package();
package.setPackage_id(Long.valueOf(w94aufnpTemp));
package.setPackage_name(w91bezei);
package.setChilds(new ArrayList<BaseCategory>());
records.add(package);
service = new Service();
service.setService_id(Long.valueOf(w94teilnTemp));
service.setService_name(w94bezei);
package.getChilds().add(service);
service.setChilds(new ArrayList<BaseCategory>());
service.setParent(package);
cardType = new KardType();
cardType.setKardType_name(w40klassTemp);
service.getChilds().add(cardType);
cardType.setChilds(new ArrayList<BaseCategory>());
cardType.setParent(service);
Package package;
for (BaseCategory pge : records) {
if (pge.getClass().getSimpleName().equals("Package")) {
package = (Package) pge;
entityManager.persist(package);
}
}
entityManager.flush();
All working fine, but if I try to remove a data set it takes a long time. My database is MySQL, and MySQL Administrator displays always "freeing items" and "removing tmp table"
Code:
entityManager.remove(package);
Any ideas?