You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Xh <xh...@yahoo.co.uk> on 2007/09/06 13:15:51 UTC
Id column = null?
Hi All!
I have build two entities atop existing database.
I have successfully created OneToMany bidirectional mapping.
The mapping is WebModules (one) - WebPage (many)
I can fetch WebModules, display their IDs and iterate through associated WebPages.
BUT WebPages IDs are set to null.
For example, the below code:
// wef is my SessionBean, it calls WebModule's NamedQuery and returns found module
WebModule module = wef.findWebModuleByName("Add");
// module id is displayed properly, e.g. 2
response.getWriter().write(
module.getId() + ":" + module.getDescription() + "<br/>");
// iterate through associated WebPages
for (WebPage page : module.getPages()) {
// id is displayed as null
// title is displayed properly
response.getWriter().write("<br/>" + page.getId());
response.getWriter().write("<br/>" + page.getTitle());
}
Of course the pages' ids are not null, they are primary keys with automatically generated identities.
I don't get it, maybe it's something wrong with my classes?
Here they are:
@Entity
@Table(name="dw_modules",schema="dw")
@NamedQuery (
name = "findWebModuleByName",
query = "SELECT wm FROM WebModule wm WHERE wm.name = :name"
)
public class WebModule implements Serializable {
private Integer id;
private Set<WebPage> pages = new HashSet<WebPage>();
...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}
...
}
@Table(name = "dw_pages", schema = "dw")
public class WebPage implements Serializable {
private Integer id;
private WebModule parentModule;
...
...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return id;
}
...
}
thanks for any hints
best regards
Łukasz
___________________________________________________________
Want ideas for reducing your carbon footprint? Visit Yahoo! For Good http://uk.promotions.yahoo.com/forgood/environment.html
Re: Id column = null?
Posted by ben short <ja...@gmail.com>.
Try changing to GenerationType to AUTO eg
@GeneratedValue(strategy = GenerationType.AUTO)
I think that the GenerationType.IDENTITY dosent assign the ids to the
rows until its commited to the db, so any relations are written to the
db as nulls.
Ben
On 9/6/07, Xh <xh...@yahoo.co.uk> wrote:
> Hi All!
>
> I have build two entities atop existing database.
> I have successfully created OneToMany bidirectional mapping.
> The mapping is WebModules (one) - WebPage (many)
>
> I can fetch WebModules, display their IDs and iterate through associated WebPages.
> BUT WebPages IDs are set to null.
>
> For example, the below code:
>
> // wef is my SessionBean, it calls WebModule's NamedQuery and returns found module
> WebModule module = wef.findWebModuleByName("Add");
>
> // module id is displayed properly, e.g. 2
>
> response.getWriter().write(
> module.getId() + ":" + module.getDescription() + "<br/>");
>
> // iterate through associated WebPages
>
> for (WebPage page : module.getPages()) {
>
> // id is displayed as null
> // title is displayed properly
>
> response.getWriter().write("<br/>" + page.getId());
> response.getWriter().write("<br/>" + page.getTitle());
> }
>
> Of course the pages' ids are not null, they are primary keys with automatically generated identities.
>
> I don't get it, maybe it's something wrong with my classes?
> Here they are:
>
> @Entity
> @Table(name="dw_modules",schema="dw")
> @NamedQuery (
> name = "findWebModuleByName",
> query = "SELECT wm FROM WebModule wm WHERE wm.name = :name"
> )
> public class WebModule implements Serializable {
> private Integer id;
> private Set<WebPage> pages = new HashSet<WebPage>();
> ...
> @Id
> @GeneratedValue(strategy = GenerationType.IDENTITY)
> public Integer getId() {
> return id;
> }
> ...
> }
>
> @Table(name = "dw_pages", schema = "dw")
> public class WebPage implements Serializable {
> private Integer id;
> private WebModule parentModule;
> ...
> ...
> @Id
> @GeneratedValue(strategy = GenerationType.IDENTITY)
> public Integer getId() {
> return id;
> }
> ...
> }
>
> thanks for any hints
> best regards
> Łukasz
>
>
>
>
> ___________________________________________________________
> Want ideas for reducing your carbon footprint? Visit Yahoo! For Good http://uk.promotions.yahoo.com/forgood/environment.html
>