简体   繁体   中英

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.

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);
}

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 .

It is happening because transient keyword stops the field to be serialized. To pass an object to JSP, it must be serialized. This field will not be there in your serialized object and hence, it is not available to JSP.

For solving your problem, you should see James' comment to use @Transient annotation. It is supplied by JPA and it should allow you to NOT save your value in DB, but serialize it so your JSP can use it.

What you are asking is not possible.. As smarter then me said before.

@Transient is just saying not to serialize/deserialize. Hibernate doesn't serialize this and the same is what jsp bean.

You can do several things:

  1. I think the best thing here is to wrap field in getter and setter in the bean.. This way you will set the property on the bean and in case of only one field every other solution will be overhead.
 public class Bean() { private Names name; private String id; public Bean() { //few action to load name this.id = name.getid(); } public String getId() { return this.id; } 
  1. Create different entity for ui(Bean) and DB(Hibernate). In many cases ui layer and entities are not the same and sometime should not be coupled
  2. You can use inheritance and create diffrent implemenation in the hibernate entity and on ui layer(Jsp bean)

If you ask me i think the best is to do what on section 1. If this happen more oftain you should consider decouple your ui layer and business entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM