Returning Child IDs using JAXRS - java

I am running into an issue with JAXRS / JAXB including child IDs in JSON results when using #Produces. Below are portions of my code. Since we are using Hibernate, I'm abstracting the id into an AbstractEntity class.
POJOs:
#XmlRootElement
public abstract class AbstractEntity implements Serializable {
private Serializable id;
#XmlElement(type-Object.class)
#XmlSchemaType(name="anySimpleType")
public final Serializable getId() {
return this.id
}
public final Serializable setId(Serializable id) {
this.id = id;
}
}
#XmlRootElement
public class Parent extends AbstractEntity {
private String parentName;
private Child child;
#XmlElement
public String getParentName() {
return parentName;
}
#XmlElement
public Child getChild() {
return child;
}
}
#XmlRootElement
public class Child extends AbstractEntity {
private String childName;
#XmlElement
public String getChildName() {
return childName;
}
}
JAXRS Services:
#Path("/parent")
public class ParentService {
#GET
#Path("/get/{id}")
#Produces(MediaType.APPLICATION.JSON)
public Parent getById(#PathParam("id") Long id) {
Parent parent = hibernateDataController.getParentById(id);
if (parent== null)
throw new NotFoundException("GET: Parent" + id + " not found.");
return parent;
}
}
#Path("/child")
public class ChildService {
#GET
#Path("/get/{id}")
#Produces(MediaType.APPLICATION.JSON)
public Child getById(#PathParam("id") Long id) {
Child child = hibernateDataController.getChildById(id);
if (child == null)
throw new NotFoundException("GET: Child " + id + " not found.");
return child;
}
}
(Note: There is more code not shown, but the main parts are above)
My project is in Eclipse, using Maven, so I fire up Jetty: mvn jetty:run
This is where the problem starts. I can access the child POJO using:
http://myserver:8080/example/child/get/1 returns->
{
"id":{"#type":"xs:long","$":"1"},
"childName":"Bart Simpson"
}
But, when I access the parent POJO, the id of the child POJO is not returned:
http://myserver:8080/example/parent/get/1 returns ->
{
"id":{"#type":"xs:long","$":"1"},
"parentName":"Homer Simpson",
"child": {
"childName":"BartSimpson"
}
}
Notice that the ID of the child is not returned, just the childName. The GUI team that I am working with is using GWT and they are requesting that I include the ID of any children in the JSON results.
Any help in getting JAXRS / JAXB to return the ID within the child JSON would be greatly appreciated. Thanks for your time.
Matt

I found my solution to the missing child IDs this morning. It actually was not a JAXRS / JAXB issue, but caused by the Hibernate mapping files (yes, I still like to use a mapping file over annotations for Hibernate).
The Hibernate mapping file for the example file above would be:
<hibernate-mapping>
<class name="com.mycompany.Parent" table="PARENT">
<id name="id" type="java.lang.Long">
<column name="PARENT_ID" scale="0" />
<generator class="native" />
</id>
<property name="parentName" type="java.lang.String">
<column name="PARENT_NAME" />
</property>
<set name="children" inverse="true" lazy="true" table="CHILD" fetch="select">
<key>
<column name="CHILD_ID" />
</key>
<one-to-many class="com.mycompany.Child" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.mycompany.Child" table="CHILD">
<id name="id" type="java.lang.Long">
<column name="CHILD_ID" scale="0" />
<generator class="native" />
</id>
<property name="childName" type="java.lang.String">
<column name="CHILD_NAME" />
</property>
<many-to-one name="parent" type="com.mycompany.Child" fetch="select">
<column name="PARENT_ID" />
</many-to-one>
</class>
</hibernate-mapping>
The fix was to force Hibernate not to 'lazy load' the children. I changed:
<set name="children" inverse="true" **lazy="true"** table="CHILD" **fetch="select"**>
to:
<set name="children" inverse="true" lazy="false" table="CHILD" **fetch="join"**>
With the modified hibernate mapping files, the IDs came through the results of JAXRS:
{
"id":{"#type":"xs:long","$":"1"},
"parentName":"Homer Simpson",
"child": {
"id":{"#type":"xs:long","$":"1"},
"childName":"BartSimpson"
}
}
Hope this helps if someone else runs into this issue.

Related

Problem with hibernate mapping (change anotations with xml)

I have a spring-hibernate application which work fine with JPA anotations, but I want to change anotations with xml configuration, but but I get this error:
org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [FROM User]
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [FROM User]
User.java
package com.fp.models;
public class User implements UserDetails {
private long id;
private String username;
private String password;
private Set<Role> authorities;
public User() {
}
User.hbm.xml
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.fp.models.User" table="users">
<id name="id" type="long" column="user_id">
<generator class="native"/>
</id>
<property name="username" column="username" type="string"/>
<property name="password" column="password" type="string"/>
<set name="authorities" table="users_roles"
inverse="false" lazy="true" fetch="select" cascade="all" >
<key>
<column name="user_id" not-null="true" />
</key>
<many-to-many entity-name="com.fp.models.Role">
<column name="role_id" not-null="true" />
</many-to-many>
</set>
</class>
</hibernate-mapping>
HibernateConfiguration.java -> https://pastebin.com/DrcGSBAp
this is the configuration which i use with the anotations
Method for test:
#Override
public int listAllUsers() {
try (Session session = sessionFactory.openSession()) {
String hql = "FROM User";
Query query = session.createQuery(hql);
if (query.list().size() == 0) {
return 0;
} else {
return 1;
}
} catch (HibernateException he) {
System.out.println(he.getMessage());
throw he;
}
}

Hibernate Single Table inheritance with self join

I have the following classes:
Guide class:
public abstract class Guide
{
private Long idGuide;
private String name;
private GuideContainer parent;
/** GETTERS & SETTERS*/
}
GuideContainer:
public class GuideContainer extends Guide
{
private Guide children;
/** GETTER & SETTERS */
}
GuideFile:
public class GuideFile extends Guide
{
private String uri;
/**GETTERS & SETTERS */
}
with the following mappings:
For Guide:
<hibernate-mapping>
<class name="Guide" table="guides" abstract="true">
<id name="idGuide" type="integer" column="idGuide">
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String" column="name" />
<discriminator column="type" type="java.lang.String" />
<many-to-one class="GuideContainer" fetch="join" name="parent">
<column name="parent" />
</many-to-one>
</class>
</hibernate-mapping>
GuideFile:
<hibernate-mapping>
<subclass extends="Guide" name="GuideFile" discriminator-value="file">
<property name="uri" type="java.lang.String" column="uri" />
</subclass>
</hibernate-mapping>
GuideContainer:
<hibernate-mapping>
<subclass extends="Guide" name="GuideContainer" discriminator-value="container">
<set fetch="select" inverse="true" lazy="true" name="children" sort="unsorted" table="children">
<key>
<column name="parent" not-null="false" />
</key>
<one-to-many class="Guide" />
</set>
</subclass>
</hibernate-mapping>
When I try to get all the guides with a given parent
Query query = getSession().createQuery("from Guide g where parent = :parent order by type").setParameter("parent", parent);
List<Guide> guides= query.list();
return guides;
I am getting the following exception:
IllegalArgumentException occurred calling getter of Guide.idGuide
And after that:
java.lang.IllegalArgumentException: object is not an instance of declaring class
What am I doing wrong?
Solved. Query should have been:
from Guide g where parent.idGuide = :parent order by type

Why am I getting Mapping Exception?

I am getting :
org.hibernate.MappingException: Foreign key (FKBB979BF4266AA123:address [a_id]))
must have same number of columns as the referenced
primary key (address [p_id,a_id])
as I try to run the following (though incomplete) snippet :
public static void main(String args[]) {
Configuration config = new Configuration().configure();
SessionFactory sessFact = config.buildSessionFactory();
Session sess = sessFact.openSession();
Transaction trans = sess.beginTransaction();
}
The hibernate mapping xml is shown below :
<class name="pojo.Person" table="person">
<id column="p_id" name="personID">
<generator class="increment" />
</id>
<property name="personName" column="p_name" />
<set name="addressSet" table="address" cascade="all">
<key column="p_id" />
<many-to-many class="pojo.Address" column="a_id" />
</set>
</class>
<class name="pojo.Address" table="address">
<id column="a_id" name="addressID">
<generator class="foreign" />
</id>
<property name="address" column="address" />
</class>
I am trying a many to many association between Person and Address class.
What is the reason for this exception ?
I have created two tables person and address using these sql commands :
CREATE TABLE person(p_id INTEGER,p_name TEXT,PRIMARY KEY(p_id));
CREATE TABLE address(a_id INTEGER,address TEXT);
POJO
Person
public class Person {
private int personID;
private String personName;
private Set addressSet;
public int getPersonID() {
return personID;
}
public void setPersonID(int personID) {
this.personID = personID;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public Set getAddressSet() {
return addressSet;
}
public void setAddressSet(Set addressSet) {
this.addressSet = addressSet;
}
Address
public class Address {
private int addressID;
private String address;
private Set personSet;
public int getAddressID() {
return addressID;
}
public void setAddressID(int addressID) {
this.addressID = addressID;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Set getPersonSet() {
return personSet;
}
public void setPersonSet(Set personSet) {
this.personSet = personSet;
}
}
For a ManyToMany Relationshhip you need a dedicated mapping table
6.2.4. Collections of values and many-to-many associations
i.e. You need something like a PersonAddress Table
CREATE TABLE personaddress (p_id integer, a_id integer)
Where p_id is a FK Reference to the Person Table and a_id a FK Reference to the Address Table
You need to specify different table name for many-to-many association as it's handled by a separate table:
<class name="pojo.Person" table="person">
<id column="p_id" name="personID">
<generator class="increment" />
</id>
<property name="personName" column="p_name" />
<set name="addressSet" table="person_address" cascade="all">
<key column="p_id" />
<many-to-many class="pojo.Address" column="a_id" />
</set>
</class>
Note that <set> now references to person_addresses table. With default configuration, Hibernate is able to create it automatically.
There's another mistake that I see: ID generator for Address entity should not be foreign, it's usually used in 1-to-1 relationships (uses ID of another associated object). You can use the same 'increment' use used for Person entity:
<class name="Address" table="address">
<id column="a_id" name="addressID">
<generator class="increment" />
</id>
<property name="address" column="address" />
</class>
You need to create a reference table:
CREATE TABLE PersonsAddresses (personId BIGINT, addressId BIGINT)
and change the mapping of the set to this:
<set name="addressSet" table="PersonsAddresses" order-by="personId">
<key column="personId" foreign-key="p_id"/>
<many-to-many column="addressId" node="a_id" class="pojo.Address" />
</set>

can we map non-object propety to an object in Many-to-one relationship of hibernate?

I have a very specific scenario as follow.
public class Person
{
Long id;
Collection<PersonRelation> personRelationCollection = new LinkedHashSet<PersonRelation>();
/**
has respective getter and setter
**/
}
public class PersonRelation
{
Long id;
Long parentPersonId; // here I don't want parentPersonId of type Person
Long childPersonId; // here also I don't want childPersonId of type Person
String relationType;
/**
has respective getter setter
**/
}
In my mapping files I have following
<class name="Person" table="PERSON">
<id name="id" column="IDENTIFIER">
<generator class="native"/>
</id>
<set
name="personRelationCollection"
table="PERSON_RELATION"
cascade="all"
>
<key column="PARENT_PERSON_ID"/>
<one-to-many class="PersonRelation"/>
</set>
</class>
and
<class name="PersonRelation" table="PERSON_RELATION">
<id name="id" column="IDENTIFIER">
<generator class="native"/>
</id>
<!-- following many-to-one mapping doesn't work-->
<!-- I need help here to satisfy my requirement -->
<many-to-one
name="parentPersonId"
column="PARENT_PERSON_ID"
class="Person"
not-null="true"/>
<Property name="childPersonId" column="CHILD_PERSON_ID"/>
<property name="relationType" column="RELATION_TYPE"/>
</class>
In this example, as in PersonRelation class, attribute parentPersonId is Long and not type of Person, I'm getting
org.hibernate.MappingException: Association references unmapped class PersonRelation
$
Please help.
Forget about references by id. In Hibernate you work with objects, not tables.
I guess your code could be written like this:
#Entity
#Table(name="your_table")
public class Item{
private Long id;
private Item parentItem;
private List<Item> children;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public Long getId(){
}
#ManyToOne()//Your options
public Item getParentItem(){
}
#OneToMane(mappedBy="parentItem")
public List<Item> getChildren(){
}
//Setters omitted
}
finally i found answer my own. Very small thing we have to do as follow.
<class name="PersonRelation" table="PERSON_RELATION">
<id name="id" column="IDENTIFIER">
<generator class="native"/>
</id>
<!-- here remove many-to-one mapping ---- it's not needed-->
<!-- treet participantPersonId as a simple property and everything will work -->
<Property name="parentPersonId" column="PARENT_PERSON_ID" type="Long"/>
<Property name="childPersonId" column="CHILD_PERSON_ID"/>
<property name="relationType" column="RELATION_TYPE"/>
</class>
This works perfectly fine. :)
Here, when you insert Person object, then it will not inset PersonRelation object too. You have to explicitly insert PersonRelation object. Perhaps, when we retrieve Person object, then it will gives you collection of PersonRelation. Here no need to retrieve PersonRelation collection explicitly.

Persisting LinkedList in Hibernate

I am trying to persist a Class with a LinkedList Attribute but can't seem to get it right. Here is my code and my mapping:
import java.util.LinkedList;
public class Stuff implements java.io.Serializable {
private long id;
private LinkedList<Image> images;
public Stuff() {
}
public Stuff(long Id) {
id = Id;
}
public long getId() {
return id;
}
public void setId(long mealId) {
id = mealId;
}
public LinkedList<Image> getNumberImages(int number) {
assert (number >= 0);
return (LinkedList<Image>) images.subList(0, number) ;
}
public LinkedList<Image> getImages() {
return images;
}
public LinkedList<Image> setImages(LinkedList<Image> images) {
this.images = images;
}
public void addImage(Image image) {
if (!images.contains(image)) {
images.add(image);
}
}
Hibernate mapping:
<hibernate-mapping>
<class name="data.Stuff" table="Stuff">
<id name="id" type="long" access="field">
<column name="ID" />
<generator class="assigned" />
</id>
<list name="images" inverse="false" table="IMAGE" lazy="true" access="field">
<key>
<column name="ID" />
</key>
<list-index></list-index>
<one-to-many class="data.Image" />
</list>
</class>
</hibernate-mapping>
It seems I can persist objects of the Class Stuff like this but when I try to recover them the following error occurres :
Hibernate: select stuff0_.ID as ID0_, stuff0_.NAME as NAME0_, meal0_.GROUPING as GROUPING0_ from MEAL meal0_
org.hibernate.PropertyAccessException: could not set a field value by reflection setter of data.Meal.images
Generally, Hibernate will provide its own implementations for collections so you should prefer interfaces to specific implementations. It's probably attempting to assign a different kind of list to images and failing. You would have to change your field to List<Image>.
Hibernate (and JPA in general) persists collections using their interfaces. The list must be declared as a List, and not as a LinkedList. And it won't be loaded with a LinkedList instance, because Hibernate uses its own List implementation to implement dirty-checking, lazy-loading, etc.
It's a good practice to program on interfaces rather than programming on concrete implementations in general. In JPA entities, it's mandatory.

Categories

Resources