Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA and JSP
JPA and JSP [message #389797] Wed, 08 July 2009 12:59 Go to next message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
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 #389799 is a reply to message #389797] Wed, 08 July 2009 13:08 Go to previous messageGo to next message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
Enrico ha scritto:
> 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


Just a point.
Actually, to solve the problem...I have for an entity with relation 2
properties for any relation. I mean, in the previous example:

@Entity
public class A {
@Id
private int ida;

@OnetoOne
private B b;

@Transient
private int bId;

}


@Entity
public class B {
@Id
private int idb;

@OnetoOne
private A a;

@Transient
private int aId;
}

so..on the JSP i set/get the transient (no persistent property).
In the set/get of the transient property I then set/get the related
persistent field, for example

public void setBId(int bid) {
this.bId = bid;

//now set the related persistent field
this.b = findbyid(bid); //this return a B object with the b id;

}


this procedure is for me boring, because I need 2 property (and 2
structures If I have onetomany or manyto many relationships: a
persistent List and an array[]).

If you have any ideas or you know some better solution please let me know.

Best regards,
Enrico
Re: JPA and JSP [message #390023 is a reply to message #389799] Wed, 08 July 2009 13:28 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 807
Registered: July 2009
Senior Member
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
Re: JPA and JSP [message #390028 is a reply to message #390023] Thu, 09 July 2009 05:55 Go to previous messageGo to next message
Enrico is currently offline EnricoFriend
Messages: 82
Registered: July 2009
Member
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 06:12 Go to previous message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 807
Registered: July 2009
Senior Member
> 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
Previous Topic:Updating an entity using reflection does not work?
Next Topic:Eclipselink with OSGi
Goto Forum:
  


Current Time: Sat Dec 20 20:23:07 GMT 2014

Powered by FUDForum. Page generated in 0.01952 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software