json - Hibernate Relation cycle via Restful -
the code this:
db:
persons(id, name)
phonenumbers(number, id_persons) fk id_person references persons(id)
java code:
package com.app.model; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.fetchtype; import javax.persistence.id; import javax.persistence.joincolumn; import javax.persistence.manytoone; import javax.persistence.table; import org.jboss.resteasy.annotations.providers.jaxb.jaxbconfig; @entity @table(name = "phonenumbers", catalog = "test") public class phonenumbers implements java.io.serializable { private static final long serialversionuid = -8122359598003938895l; private string number; private persons persons; public phonenumbers() { } public phonenumbers(string number) { this.number = number; } public phonenumbers(string number, persons persons) { this.number = number; this.persons = persons; } @id @column(name = "number", unique = true, nullable = false, length = 20) public string getnumber() { return this.number; } public void setnumber(string number) { this.number = number; } @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "id_person") public persons getpersons() { return this.persons; } public void setpersons(persons persons) { this.persons = persons; } } package com.app.model; import java.util.hashset; import java.util.set; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.fetchtype; import javax.persistence.generatedvalue; import static javax.persistence.generationtype.identity; import javax.persistence.id; import javax.persistence.onetomany; import javax.persistence.table; @entity @table(name = "persons", catalog = "test") public class persons implements java.io.serializable { private static final long serialversionuid = -3247756369663156186l; private integer id; private string name; private set<phonenumbers> phonenumberses = new hashset<phonenumbers>(0); public persons() { } public persons(string name) { this.name = name; } public persons(string name, set<phonenumbers> phonenumberses) { this.name = name; this.phonenumberses = phonenumberses; } @id @generatedvalue(strategy = identity) @column(name = "id", unique = true, nullable = false) public integer getid() { return this.id; } public void setid(integer id) { this.id = id; } @column(name = "name", nullable = false, length = 50) public string getname() { return this.name; } public void setname(string name) { this.name = name; } @onetomany(fetch = fetchtype.lazy, mappedby = "persons") public set<phonenumbers> getphonenumberses() { return this.phonenumberses; } public void setphonenumberses(set<phonenumbers> phonenumberses) { this.phonenumberses = phonenumberses; } } package com.app.dao; import javax.ejb.stateless; import javax.persistence.entitymanager; import javax.persistence.persistencecontext; import javax.persistence.persistencecontexttype; import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; import com.app.model.phonenumbers; @stateless public class phonenumbershome { private static final log log = logfactory.getlog(phonenumbershome.class); @persistencecontext(unitname="phonebook", type=persistencecontexttype.extended) private entitymanager entitymanager; public void persist(phonenumbers transientinstance) { log.debug("persisting phonenumbers instance"); try { entitymanager.persist(transientinstance); log.debug("persist successful"); } catch (runtimeexception re) { log.error("persist failed", re); throw re; } } public void remove(phonenumbers persistentinstance) { log.debug("removing phonenumbers instance"); try { entitymanager.remove(persistentinstance); log.debug("remove successful"); } catch (runtimeexception re) { log.error("remove failed", re); throw re; } } public phonenumbers merge(phonenumbers detachedinstance) { log.debug("merging phonenumbers instance"); try { phonenumbers result = entitymanager.merge(detachedinstance); log.debug("merge successful"); return result; } catch (runtimeexception re) { log.error("merge failed", re); throw re; } } public phonenumbers findbyid(string id) { log.debug("getting phonenumbers instance id: " + id); try { phonenumbers instance = entitymanager.find(phonenumbers.class, id); log.debug("get successful"); return instance; } catch (runtimeexception re) { log.error("get failed", re); throw re; } } } package com.app.dao; import javax.ejb.stateless; import javax.persistence.entitymanager; import javax.persistence.persistencecontext; import javax.persistence.persistencecontexttype; import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; import com.app.model.persons; @stateless public class personshome { private static final log log = logfactory.getlog(personshome.class); @persistencecontext(unitname="phonebook", type=persistencecontexttype.extended) private entitymanager entitymanager; public void persist(persons transientinstance) { log.debug("persisting persons instance"); try { entitymanager.persist(transientinstance); log.debug("persist successful"); } catch (runtimeexception re) { log.error("persist failed", re); throw re; } } public void remove(persons persistentinstance) { log.debug("removing persons instance"); try { entitymanager.remove(persistentinstance); log.debug("remove successful"); } catch (runtimeexception re) { log.error("remove failed", re); throw re; } } public persons merge(persons detachedinstance) { log.debug("merging persons instance"); try { persons result = entitymanager.merge(detachedinstance); log.debug("merge successful"); return result; } catch (runtimeexception re) { log.error("merge failed", re); throw re; } } public persons findbyid(integer id) { log.debug("getting persons instance id: " + id); try { persons instance = entitymanager.find(persons.class, id); log.debug("get successful"); return instance; } catch (runtimeexception re) { log.error("get failed", re); throw re; } } } package com.app.ws; import javax.ejb.ejb; import javax.naming.context; import javax.naming.initialcontext; import javax.ws.rs.pathparam; import javax.ws.rs.produces; import javax.ws.rs.get; import javax.ws.rs.path; import com.app.dao.personshome; import com.app.model.persons; @path("/persons") public class personsresources { @ejb private personshome ph; @get() @produces("application/json") @path("/{id}") public persons getperson(@pathparam("id") int id) { try{ context cx = new initialcontext(); ph = (personshome)cx.lookup("java:module/personshome"); }catch(exception e){ e.printstacktrace(); } return ph.findbyid(id); } }
and persistence.xml
<?xml version="1.0" encoding="utf-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 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_2_0.xsd"> <persistence-unit name="phonebook" transaction-type="jta"> <provider>org.hibernate.ejb.hibernatepersistence</provider> <jta-data-source>java:/test</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.mysqldialect" /> <property name="hibernate.hbm2ddl.auto" value="validate"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.jbosstransactionmanagerlookup"/> </properties> </persistence-unit> </persistence>
on link:
http://localhost:8080/phonebook/persons/1
i aspect ad output this:
{"id":1,"name":"roberto","phonenumberses":[{"number":"+01932932"......
but i've cycle this:
{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":{"id":1,"name":"roberto","phonenumberses":[{"number":"01932932","persons":
anyone can halp me?
try using @jsonidentityinfo(generator = objectidgenerators.intsequencegenerator.class, property = "@id"
check out for more info
Comments
Post a Comment