-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Persist + ManyToOne + return actual data
PostPosted: Wed Feb 27, 2013 12:24 pm 
Newbie

Joined: Wed Feb 27, 2013 12:00 pm
Posts: 1
Hello all,

Here is simplified version of what i have:

Code:
class A {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @ManyToOne(optional=false)
    private B b;
    @Column
    private String label;
}

class B {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column
    private String name;
}

DAO:
...
public void create(final T entity) {
        this.getCurrentSession().persist(entity);
}
public void  update(final T entity) {
        this.getCurrentSession().merge(entity);
}
...


Basically i have a REST service and often i get A instances populated with B, and B has id only (no B name field):
Assume that in DB i got B.id=1 and B.name="testname";
Code:
// A instance
{
    label: "test",
    b: {
            id: 1
    } 
}


After that i call dao.create method on the backend and return saved a instance (in service layer)
Here is what i got as result:
Code:
// A instance that returned after dao.create
{
    id: 11,
    label: "test",
    b: {
            id: 1,
            name: null
    } 
}


So my question is how to force hibernate populate other B information (B name field in this example)?

Excpected:
Code:
// A instance that returned after dao.create
{
    id: 11,
    label: "test",
    b: {
            id: 1,
            name: "testname"
    } 
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.