Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature.SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS customise generated name #50

Closed
jackmatt2 opened this issue Jan 31, 2014 · 11 comments · Fixed by #96
Closed

Feature.SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS customise generated name #50

jackmatt2 opened this issue Jan 31, 2014 · 11 comments · Fixed by #96

Comments

@jackmatt2
Copy link

It would be good if Feature.SERIALIZE_IDENTIFIER_FOR_LAZY_NOT_LOADED_OBJECTS would let you customise the generated name, or at least use the name from the java bean.

Currently it is serializing using full package and class name:

"client":{"com.test.domain.Client":1}

I want it to use the id instead (which is the name of the attribute in the hibernate entity):

"client":{"id":1}

Maybe there is a way around this but I can't find any.

@cowtowncoder
Copy link
Member

Code in question is in HibernateProxySerializer; there is no way to customize it, but we are open to suggestions for improvements. I think this was a contributed feature so I don't know enough to improve handling.

@jackmatt2
Copy link
Author

I was thinking that an annotation might be the way to go.

@SerializeLazyIdentifierAs(value="id")
private Client client;

In this case the id attribute for client would be serialized as "id" instead of "com.test.domain.Client"

Or possibly a better place might be on the Client entity itself.

@SerializeLazyIdentifierAs(value="id")
@Identifier
private long id;

@cowtowncoder
Copy link
Member

Ok, use of annotations sounds reasonable enough, and should be accessible.

A simple unit test or sample classes would be useful. I am pretty good at merging contributions as well. :)

@srstsavage
Copy link

@jackmatt2 @cowtowncoder While custom names would be nice, there is currently a way to use the id's actual attribute name instead of the full package and class name. If the Hibernate session is still open when the bean is serialized the real attribute name will be used. If the session is closed, the package and class name are used instead.

nik-kashi pushed a commit to nik-kashi/jackson-datatype-hibernate that referenced this issue Mar 17, 2015
First try for identifier field (that annotated with @id)
If Id field present, id field name used ad key instead of class name.
@Mohamed-Mamdouh
Copy link

Hello Guys.
Am facing the same issue regarding the naming of the Ids and wonder if any implemented the recommended fix ?

@LugaMuga LugaMuga mentioned this issue Sep 4, 2015
@bedag-moo
Copy link
Contributor

It is worth noting that the HibernateSerializer uses the actual name of the id property if the session is still open, or the hibernate mapping is passed to the Hibernate4Module constructor.

Still, it would be nice if the Serializer could infer this information automatically in all cases.

@cowtowncoder
Copy link
Member

At this point, what I really need is a unit test that shows expected behavior; solutions are only useful once problem is shown.

@bedag-moo
Copy link
Contributor

I don't have time to write a full test, but I can clarify the behaviour I would expect, and that @jackmatt2 probably means when he says "use the property from the java bean":

Given an entity

@Entity
public class MyEntity {
    @Id
    private String id;

    public String getId() {
        return id;
    }

    public String setId(String id) {
        this.id = id;
    }
}

or an entity

@Entity
public class MyEntity {
    private String id;

    @Id
    public String getId() {
        return id;
    }

    public String setId(String id) {
        this.id = id;
    }
}

or an entity

@Entity
public class MyEntity extends BaseEntity {
}

where

@MappedSuperclass
public abstract class BaseEntity {
    @Id
    private String id;

    public String getId() {
        return id;
    }

    public String setId(String id) {
        this.id = id;
    }
}   

the following should pass:

EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
    // an uninitialised lazy loading proxy
    proxy = entityManager.getReference(MyEntity.class, "42"); 
} finally {
    // is detached
    entityManager.close();                                                
}

// but can still be marshalled correctly
assertEquals("{"id":"42"}", objectMapper.writeValueAsString(proxy));

@cowtowncoder
Copy link
Member

@bedag-moo Perhaps someone can find time to work on this, either on writing the test, or working using other means to resolve it. I do not use Hibernate and my time is spread across many jackson modules, so this will not be high priority.

nik-kashi pushed a commit to nik-kashi/jackson-datatype-hibernate that referenced this issue Jun 11, 2016
@cm325
Copy link

cm325 commented Jul 26, 2016

It would appear that this actually does work, as long as you instantiate the Hibernate4Module with the hibernate mappings:

http://stackoverflow.com/questions/21472926/override-id-name-generated-by-jackson-datatype-hibernate

What I can't figure out is how to get a handle on the LocalSessionFactoryBean in a spring boot app in order to provide the mappings. Any ideas?

@cm325
Copy link

cm325 commented Jul 27, 2016

Oh, nevermind, I realized I can cast the sessionFactory to "Mappings" and it works. I used this technique to get the sessionFactory in spring boot - http://stackoverflow.com/a/33881946/228369

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants