6

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.

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
Daniel Newtown
  • 2,873
  • 8
  • 30
  • 64
  • and where is ***transient field*** in your posted Code ? – Neeraj Jain Apr 16 '15 at 06:24
  • @NeerajJain it is a joinColumn defined over the class. question is updated, although it does not matter as I have getters and setters for transient. Imagine there is no field at all JSP should have access to getter methods. – Daniel Newtown Apr 16 '15 at 06:30
  • 1
    The entire point of a transient field is that it is not managed by Hibernate. It's value is not persisted, saved and can't be retrieved because it shouldn't be going into the database. See [here](http://stackoverflow.com/questions/2154622/why-does-jpa-have-a-transient-annotation) for specifics – JamesENL Apr 16 '15 at 06:37
  • @James then how to solve my issue? – Daniel Newtown Apr 16 '15 at 06:47
  • 1
    @Daniel, please check and fix typo mistake for 'public String getid()' [I expect 'getId()'] Moreover, you should provide more details, because what you wrote seems impossible. For example try to check scriplet in JSP: <% out.println(((Names)((Mystudents)request.getAttribute("mystudents")).getNames().iterator().next()).getId()); %> with fully qualified class names – michaldo Apr 16 '15 at 10:33
  • @michaldo that is not an scriplet thats my java code. – Daniel Newtown Apr 16 '15 at 23:14
  • What's the error you're getting? or is just blank? If you have a field that needs to be populated on the fly through hibernate, then you need a hibernate session to be open while the jsp is processing. http://java.dzone.com/articles/open-session-view-design – Ashley Frieze Apr 16 '15 at 23:19
  • @Daniel when you execute the scriplet on JSP page you will reproduce your Java code identically in JSP page. The scriplet will verify if field you want to display exists or you access it wrong way – michaldo Apr 17 '15 at 06:35
  • @michaldo Ive noticed that JSP does not have access to the transient (if thats what you mean) but to have the correct making with hibernate I need them to be transient just wondering if there is a work around the issue. – Daniel Newtown Apr 18 '15 at 00:32
  • @AshleyFrieze it does not have access to the field at all seems like it cannot see it. – Daniel Newtown Apr 18 '15 at 00:33
  • @Daniel, the problem is that Transient is visible only to Hibernate. Other components, especially JSP, ignores the annotation. Because Hibernate do not touch Transient attributes, I expect that you set attribute value manually, display the value in Java code but you can't display it in JSP. So that must be bug in your code. And you must track the bug yourself. I only can suggest you how to track. Because I know for JSP does not matter if attribute is transient or not, I suggest you to focus on Java code and EL expression – michaldo Apr 18 '15 at 09:50
  • @michaldo I do not really get it, some people say JSP cannot see Transient some say it can. I am puzzled. What do you mean by bug, I do not get that. The code is as simple as I put in question what other part would you like me to include in the question? I think those who say JSP cannot see the transient fields are right, only fields that my JSP page cannot access are those transient ones. The rest are easily accessible. – Daniel Newtown Apr 18 '15 at 10:19
  • @Daniel, what annotation do you use: javax.persistence.Transient or java.beans.Transient? – michaldo Apr 18 '15 at 10:50
  • @DanielNewtown - "jsp does not have access" isn't 100% unambiguous. It's likely that the field hasn't been populated yet. If you want it populated, then do a get on it before returning from your controller - that might do it. If you don't want to populate it unless the view needs it, then open a session in the view as described in my comment's link. Or define a simpler pojo to populate from your controller that's not a hibernate entity - avoiding the whole issue. – Ashley Frieze Apr 18 '15 at 11:08
  • @AshleyFrieze what do you mean by do a get? I am confused a bit would you please clarify – Daniel Newtown Apr 18 '15 at 11:33
  • 1
    Dear @Daniel, you must better cooperate if want your problem be solved. You probably made a typo error: in example is getid (lowercase). I asked you for check it 2 days ago, you ignored it. Below Vlad Mihalcea spent his time and prepared answer basing on typo error IMHO. You are told many times that problem is not related to Hibernate Transient, but you still dig that area. I suspect you use by mistake java.beans.Transient annotation. I asked you to verify that. Very simple verification, but you also ignored it. You encourage community by bounty: please let us help you and cooperate better – michaldo Apr 18 '15 at 20:46
  • 1
    @michaldo I am really sorry for the delay I have an issue on my system cannot boot it up. Will update you asap. sorry again for any inconvenience caused. – Daniel Newtown Apr 19 '15 at 10:06
  • From the code snippet the Transient annotation is a red herring. The property Id is named incorrectly. Vlad's answer is correct. Beans are not serialised when given to jsp and even if they were the Transient annotation would not affect that. Transient is a hint to hibernate not to map the field into the database. – Ashley Frieze Apr 19 '15 at 11:25
  • Maybe it is just a lifecycle issue. What is the scope of the bean within the JSP? Have you tried to request the page several times. – Diversity Apr 21 '15 at 18:04

4 Answers4

3

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);
}
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
3

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Jeff
  • 3,669
  • 1
  • 23
  • 33
0

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.

Aakash
  • 2,029
  • 14
  • 22
  • Thanks, I know it is because of the transient annotation but how to solve the issue, due to hibernate I need to keep them transient. – Daniel Newtown Apr 16 '15 at 06:46
  • Since you cannot do it directly, you should create a new String object with this field's value where you are passing your object to JSP. There, you also pass this new string object and use that in your JSP. – Aakash Apr 16 '15 at 06:55
  • Please remember to mark this as an answer if it solved your problem. – Aakash Apr 16 '15 at 10:40
  • Thanks for your answer, I am waiting to see if there would be any better solution (hibernate related) otherwise will definitely accept your answer. – Daniel Newtown Apr 16 '15 at 23:15
  • " To pass an object to JSP, it must be serialized" This is simply FALSE. The answer should be deleted – michaldo Apr 17 '15 at 06:30
  • @michaldo OK. What is your answer then? – Aakash Apr 17 '15 at 06:47
  • @Aakash My answer is that the case is impossible. As that answer has no chance to be accepted, Daniel must provide more details to track the reason. My first hint is execute Java code on JSP page, as I proposed in comment to question. – michaldo Apr 17 '15 at 07:20
  • Ok. In that case, let the OP provide more details and let us help him solve his problem. After that, I will delete my answer for sure. But before that, I will try to dig some more about that. – Aakash Apr 17 '15 at 07:24
  • @vlad is right. The only thing wrong with the getId method is that it doesn't follow the java bean convention. Jstl can only access java bean properties. – Robert Moskal Apr 19 '15 at 04:46
  • @Robert-Moskal, I think vlad is wrong. I made experiment on Tomcat with lowercase [getid] and when I tried to get ${x.id} I got exception that there is no method [getId] (camelcase) – michaldo Apr 19 '15 at 10:17
  • getId() is what you want. – Robert Moskal Apr 19 '15 at 11:32
0

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.

Aviad
  • 1,539
  • 1
  • 9
  • 24