I'd like to load objects from a database and then send them to client as XML-objects.
The objects that I load from database contain sets of other objects. Therefore Hibernate converts those objects to instances of org.hibernate.collection.PersistentMap. The loaded objects work as intended when I call their methods, but when I convert them to XML they contain attributes and references to org.hibernate.collection.PersistentMap. The client can't convert the XML back to java object if it contains stuff from org.hibernate.collection.PersistentMap. How do I remove them?
I'm loading User information from database and I want to send it to my remote admin client.
This is the kind of XML message that I currently get:
Code:
<dbmodel.User>
<userights class="org.hibernate.collection.PersistentMap">
<initialized>true</initialized>
<owner class="dbmodel.User" reference="../.."/>
<cachedSize>-1</cachedSize>
<role>dbmodel.User.userights</role>
<key class="string">joku</key>
<dirty>false</dirty>
<storedSnapshot class="map">
<entry>
<int>1</int>
<dbmodel.UseRight>
<serialkey>S-1</serialkey>
<product>
<productid>1</productid>
<productname>Tuote 1</productname>
</product>
</dbmodel.UseRight>
</entry>
<entry>
<int>2</int>
<dbmodel.UseRight>
<serialkey>S-2</serialkey>
<product>
<productid>2</productid>
<productname>Tuote 2</productname>
</product>
</dbmodel.UseRight>
</entry>
</storedSnapshot>
<map>
<entry>
<int>1</int>
<dbmodel.UseRight reference="../../../storedSnapshot/entry/dbmodel.UseRight"/>
</entry>
<entry>
<int>2</int>
<dbmodel.UseRight reference="../../../storedSnapshot/entry[2]/dbmodel.UseRight"/>
</entry>
</map>
</userights>
<username>joku</username>
<firstname>jaska</firstname>
<lastname>Jokunen</lastname>
<password>passu</password>
</dbmodel.User>
This is the kind of XML message that I want:
Code:
<dbmodel.User>
<userights class="hashtable">
<entry>
<int>2</int>
<dbmodel.UseRight>
<serialkey>S-2</serialkey>
<product>
<productid>2</productid>
<productname>Tuote 2</productname>
</product>
</dbmodel.UseRight>
</entry>
<entry>
<int>1</int>
<dbmodel.UseRight>
<serialkey>S-1</serialkey>
<product>
<productid>1</productid>
<productname>Tuote 1</productname>
</product>
</dbmodel.UseRight>
</entry>
</userights>
<username>joku</username>
<firstname>jaska</firstname>
<lastname>Jokunen</lastname>
<password>passu</password>
</dbmodel.User>
Here is the source code for the objects that are saved to a database:
User.java
Code:
package dbmodel;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.CascadeType;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.FetchType;
import java.util.Map;
import java.util.Hashtable;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
@Entity
@Table(name="UserTable")
public class User implements Serializable{
private Map<Integer, UseRight> userights=new Hashtable();
private String username;
private String firstname;
private String lastname;
private String password;
public User(){}
public User(String username, String password, String firstname, String lastname) {
this.username = username;
this.firstname = firstname;
this.lastname = lastname;
this.password = password;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Id
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@OneToMany(fetch= FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "userightlist", joinColumns = { @JoinColumn(name = "username") }, inverseJoinColumns = { @JoinColumn(name = "serialkey") })
public Map<Integer, UseRight> getUserights() {
return userights;
}
public void setUserights(Map<Integer, UseRight> userights) {
this.userights= userights;
}
public void addUseRight(UseRight ur){
userights.put(ur.getProduct().getProductid(), ur);
}
public void addUseright(Product product){
userights.put(product.getProductid(), new UseRight(("S-"+product.getProductid()),product));
}
public boolean checkUseRight(int productid){
if(userights.get(productid)==null)
return false;
return true;
}
public boolean checkUseRight(Product product){
if(userights.get(product.getProductid())==null)
return false;
return true;
}
}
UseRight
Code:
package dbmodel;
import java.io.Serializable;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.FetchType;
import javax.persistence.CascadeType;
@Entity
public class UseRight implements Serializable{
private String serialkey;
private Product product;
public UseRight(){}
public UseRight(String serialkey, Product product) {
this.serialkey = serialkey;
this.product = product;
}
@OneToOne(fetch= FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name="Product")
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
@Id
public String getSerialkey() {
return serialkey;
}
public void setSerialkey(String serialkey) {
this.serialkey = serialkey;
}
This is the code I use to load the users from database
Code:
public static List getUsers(){
try{
Session session = sessionFactory.openSession();
session.beginTransaction();
Query query = session.createQuery("from User");
List list = query.list();
session.getTransaction().commit();
session.close();
return list;
}catch(Exception e){
e.printStackTrace();
}
}