JPA and JSP [message #389797] |
Wed, 08 July 2009 08:59  |
Eclipse User |
|
|
|
Dear all,
I would like to use jpa on my JSP in order to manage persistance. My
problem is with Entity relationship.
JavaBean on JSP allow to set/get primitive java types. So for example I
can set by <input> tag on JSP a javabean property.
But if I have two Entity A and B with a relation between them (the
following source is just for example...it can have sintax error..but it
is just to give you an example):
@Entity
public class A {
@Id
private int ida;
@OnetoOne
private B b;
}
@Entity
public class B {
@Id
private int idb;
@OnetoOne
private A a;
}
and I use this bean in the JSP:
<jsp:useBean id="a"class="A" scope="page" />
<jsp:setProperty name="a" property="*"/>
how can I set the property b of class A?
If it was a primitive type (like an int) I could:
<input type="hidden" name="b" value="<%....%>">
but it is not a primitive type..it is an object of class B (an entity).
There is any way to use javabean entity with relationship propertis
(also OneToMany...with list for example) on JSP beans?
Best Regards,
Enrico
|
|
|
|
|
Re: JPA and JSP [message #390028 is a reply to message #390023] |
Thu, 09 July 2009 01:55   |
Eclipse User |
|
|
|
tbee ha scritto:
> I have no direct experience with JSP and JPA, but I'll just yell some
> generics. Maybe it will trigger an idea.
>
> First, a hidden field practically can only contain a prirary key value;
> your B-id. And when you set the property, you need to execute the find
> to convert the id to an entity. As you show in the second post, this
> could be included in the entity itself, but that is not what you want.
> So then the find must be done in the JSP page.
>
> However no JSP binding logic will do that find for you, so you need to
> do this manually and that means you cannot use any automatic binding.
> From that perspective a special setter which does a find may not be a
> bad idea; the find code has to go somewhere and putting it in the entity
> at least makes it reusable.
>
> Tom
>
>
>
Tom,
thank you for yopur reply.
So, you mean is better to find in the jsp and so set the persistent
property from jsp?I would like to have less possible logic (java code)
in JSP pages. I would like just to have data presentation and data input
on JSP pages, while all the logic is in java beans and core.
I use java generics to make persissten operation (load, save, find
etc..), so I have this method (no in the entity but in a baseDAO
class....then any entity has an own dao class that extend the base dao
and, basing on generics, is specific for the related entity).
Just a point...I know there is a jpa-tag library for JSP
( http://weblogs.java.net/blog/ss141213/archive/2006/01/custom _tags_to.html
and https://jpa-taglib.dev.java.net/). Do you think it could be useful
and solve my problem?
Best Regards,
Enrico
|
|
|
Re: JPA and JSP [message #390029 is a reply to message #390028] |
Thu, 09 July 2009 02:12  |
Eclipse User |
|
|
|
> So, you mean is better to find in the jsp and so set the persistent
> property from jsp?I would like to have less possible logic (java code)
> in JSP pages. I would like just to have data presentation and data input
> on JSP pages, while all the logic is in java beans and core.
> I use java generics to make persissten operation (load, save, find
> etc..), so I have this method (no in the entity but in a baseDAO
> class....then any entity has an own dao class that extend the base dao
> and, basing on generics, is specific for the related entity).
>
> Just a point...I know there is a jpa-tag library for JSP
> ( http://weblogs.java.net/blog/ss141213/archive/2006/01/custom _tags_to.html
> and https://jpa-taglib.dev.java.net/). Do you think it could be useful
> and solve my problem?
What I'm trying to say is that somewhere between the id that is stored in the form and the setter of your entity, you have to do a "find". You can put that find in the JSP or in directly the entity; it is the same logic to codde, but when you put it in the entity you can reuse it.
What will not work is the automatic binding of JSP; if JSP automatically writes a submitted value to a setter in a bean, it will never do the find. So you cannot use JSP binding.
You can use the JPA tag libraries you mention to implement the find, but then you still have to do it yourself, see the examples of the tag libraries:
<% String name = String.class.cast(request.getParameter("name")); %>
<jpa:injectDefaultPC id="em1"/>
<jpa:find em="em1" clazz="example.UserCredential" pk="<%= name %>" id="credential"/>
Would you rather have that in the JSP or this in your entity:
public void setB(int id)
{
setB( findbyid(id) );
}
Personally I go for the last.
Tom
|
|
|
Powered by
FUDForum. Page generated in 0.29382 seconds