Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Because of Hibernate Mapping need to have some of the fields as @Transient but JSP does not have access to them

In Java, I have access to value of Transient fields of the class. However, I do not access to the same fields on JSP. How can I make them available to JSP?

I am retrieving the values using Hibernate, I reckon a solution to this would be to Transformers.aliasToBean option but is there any other solution to it?

Is there anyway to get rid of transient annotation but have the same mapping in Hibernate? In that case, the problem will be solved.

@AssociationOverrides({
        @AssociationOverride(name = "tta.names", joinColumns = @JoinColumn(name = "id"))})
public class Names implements java.io.Serializable {

    private static final long serialVersionUID = -30956546435023625398L;

    @EmbeddedId
    private TableToAssociate tta = new TableToAssociate();


    @Transient
    public String getid() {
        return tta.getMyIds().getId();
    }

    public void setid(String id) {
        this.tta.getMyIds().setId(id);
    }

In Java, I can access them using following code

     System.out.println(mystudents.getNames().iterator().next().getId());

In JSP, I do not have access to them!

    <c:forEach var="nm"items="${mystudents.names}">
                    ${nm.id}
                </c:forEach>

If I put another field of names that is not transient, JSP successfully show the value of that item.

like image 535
Daniel Newtown Avatar asked Apr 16 '15 06:04

Daniel Newtown


2 Answers

Try renaming the methods to match the JavaBean specification.

Instead of:

@Transient
public String getid() {
    return tta.getMyIds().getId();
}

public void setid(String id) {
    this.tta.getMyIds().setId(id);
}

you should have:

@Transient
public String getId() {
    return tta.getMyIds().getId();
}

public void setId(String id) {
    this.tta.getMyIds().setId(id);
}
like image 118
Vlad Mihalcea Avatar answered Sep 28 '22 18:09

Vlad Mihalcea


Get rid of @Transient on your entity. Based on your embedded id, you've chosen field annotations. You should be able to have a getter that Hibernate won't try to persist without explicitly marking it as such. And change the getter/setter to use correct JavaBean syntax. getId instead of getid.

like image 20
Jeff Avatar answered Sep 28 '22 19:09

Jeff