0

I have a entity class "classA" and a model class "ClassA1". ClassA is used to store the values in mysql database from server side and ClassA1 is used to convert a API response string into object which is client side.

Now I am having two java classes with same getters and setters but ClassA contains hibernate annotations and ClassA1 is simply a POJO. Consider the following class structures

ClassA.java

@Entity
@Table(name="classA")
public class ClassA {
    @Id
    private int id;
    private String name;

    public int getId() {
        return id
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name="name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

ClassA1.java

public class ClassA1 {
    private int id;
    private String name;

    public int getId() {
        return id
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

My application is containg more number of classes like the above. I want to avoid this, because If I add any column to my database, currently I am adding getters and setters in both ClassA and also in ClassA1.

Is there any way to use a single class for both server and client side modules?

5
  • 1
    Whats the problem you are facing in having both as same? Sep 22, 2016 at 9:27
  • If I use a same class, client side model also loads a jpa related code, that really I don't want.
    – Achaius
    Sep 22, 2016 at 9:32
  • @Achaius what JPA related code is loaded? Sep 22, 2016 at 9:36
  • All javax.persistence.* from ClassA is loading in client side model which is not needed by a client. Client side code simply wants to convert a JSON response string into a object to use further.
    – Achaius
    Sep 22, 2016 at 9:42
  • That's just annotations that does nothing! Sep 24, 2016 at 0:59

2 Answers 2

1

You can simply try to use same class in both situation. It should work. That's the way I tried in the beginning of one of my earlier project. As long as you can carefully deal with JPA merging of the ClassA you received from client-side by treating that as a detached entity.

However, when your entity design becoming more complicated, you will face a lot of problem (and that's why I switch away from this approach in my project :P). One of the biggest problem is, given you have modelled the entity relationships, once you try to "serialize" it for client use, a lot of (JAXB etc, at that time I was doing the projects) solution is going to recursively trace through all the relationship and try to convert it. Such behavior will trigger massive lazy fetching of entities, and make the result serialized form contains huge amount of (unused) data. Although you can control the behavior (by adding some kind of ignore annotation etc), the result entity will become messy to maintain.

Therefore what you are doing, imho, is not unreasonable. What you need to beware is, treat the value object (what you called "Model") something for "presentation". You don't need to make it strictly the same as your entities.

Make use of / develop some util library to handle the construction of value object from entities, and populating data from value object back to entities.

Of course, the suggestion may not apply to you, given that you haven't share much on your architecture.

1

You could specify your Hibernate mappings separately in an XML file instead of using annotations. This is the old way of doing it, nowadays most people use the annotations because it is more convenient and follows the JPA standard (mostly).

Another solution is to use a bean mapping framework like Dozer to make the mapping easier.

It's quite common to separate your persistent (JPA) Entities and value objects used by views in your architecture. You can customize the value objects for the view in which they are used. Maybe the view does not need the full User entity, but only the id, name and address? If this is the case it makes the communication between view and backend lighter and partially resolves the duplication between your ValueObjects and persistent entities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.