|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Bannato
Iscritto dal: Apr 2006
Messaggi: 5857
|
[JAVA] DTO/Entity come ritornare/gestire i sottoggetti ?
Stavo implementando un layer di accesso alla base dati usando dei DTO (anche se sarebbero più degli Entity Beans) con relative classi DAO che usano JDBC (JPA/Hibernate scartati dopo mesi di litigi e grosse difficoltà ad adattarli a DB esistenti) e mi sono venuti dei dubbi (scarsa esperienza sulla cosa) su come gestire il passaggio degli oggetti non primitivi.
Facciamo un esempio con una classe DTO che contiene 2 oggetti non primitivi: Codice:
public class MyDTO implements Serializable {
@NotNull
private Key1 pKey = new Key1();
private String description;
@NotNull
private String attribute1;
@Min(value=0)
private int quantity;
@NotNull
private Key categoryFKey = new Key2();
public MyDTO () {
}
...
}
Codice:
public class Key1 implements Serializable {
@Min(value=1)
private int id;
@NotNull @Size(min=1, max=2)
private String type;
public Key1 () {
}
public Key1 (int id, String type) {
this.id= id;
this.type = type;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id= id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public int hashCode() {
...
}
@Override
public boolean equals(Object object) {
...
}
@Override
public String toString() {
...
}
}
Codice:
public class Key2 implements Serializable {
@NotNull @Size(min=1, max=4)
private String category;
@NotNull @Size(min=1, max=2)
private String subCategory;
public Key2 () {
}
public Key2 (String category, String subCategory) {
this.category= category;
this.subCategory= subCategory;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category= category;
}
public String getSubCategory() {
return subCategory;
}
public void setSubCategory(String subCategory) {
this.subCategory= subCategory;
}
@Override
public int hashCode() {
...
}
@Override
public boolean equals(Object object) {
...
}
@Override
public String toString() {
...
}
}
Ossia ritornare una copia del riferimento all'oggetto contenuto o clonare questo oggetto e ritornare il riferimento alla copia. Codice Soluzione 1: Codice:
public Key1 getPK() {
return pKey;
}
public void setPK(Key1 pKey) {
this.pKey= pKey;
}
public Key2 getCategoryFKey() {
return categoryFKey;
}
public void setCategoryFKey(Key2 fKey) {
this.categoryFKey = fKey;
}
Codice:
public Key1 getPK() {
Key1 clone = new Key1();
clone.setId(pKey.getId());
clone.setType(pKey.getType());
return clone;
//Oppure in modo meno prolisso un
// return new Key1(pKey.getId(), pKey.getType());
}
public void setPK(Key1 pKey) {
this.pKey.setId(pKey.getId());
this.pKey.setType(pKey.getType());
}
public Key2 getCategoryFKey() {
return new Key2(categoryFKey.getCategory(), categoryFKey.getSubCategory();
}
public void setCategoryFKey(Key2 fKey) {
this.categoryFKey.setCategory(fKey.getCategory());
this.categoryFKey.setSubCategory(fKey.getSubCategory());
}
Quindi qual'è l'approccio migliore ? C'è una terza possibilità ? Ultima modifica di FabryHw : 25-07-2011 alle 20:19. |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 12:26.



















