-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: NPE with @Id on @OneToOne
PostPosted: Fri Feb 09, 2007 1:38 pm 
Newbie

Joined: Fri Feb 09, 2007 1:23 pm
Posts: 3
Hi there,

I hope it's not a misuse of Hibernate, but I'm trying to use @Id on @OneToOne, and this causes the following NPE when running hbm2ddl:
Quote:
java.lang.NullPointerException
at org.hibernate.util.StringHelper.qualify(StringHelper.java:264)
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:196)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
at org.hibernate.tool.ant.ConfigurationTask.getConfiguration(ConfigurationTask.java:56)
at org.hibernate.tool.ant.HibernateToolTask.getConfiguration(HibernateToolTask.java:287)
at org.hibernate.tool.ant.Hbm2DDLExporterTask.execute(Hbm2DDLExporterTask.java:45)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:171)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.Main.runBuild(Main.java:668)
at org.apache.tools.ant.Main.startAnt(Main.java:187)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)



What I'm trying to do at the application level is:
Quote:
Foo class and table
long foo_id, primary key
FooData fooData, nulllable=true


Quote:
FooData class and table
foo_id references Foo(foo_id), nullable=false


This is expressed in Java as:
Code:
@Entity
@Table(name = "foo")
public class Foo {
   @Id @GeneratedValue
   @Column(name = "foo_id")
   Long id;

   @OneToOne(mappedBy = "foo", cascade = CascadeType.ALL)
   FooData fooData;
...
@Entity
@Table(name = "foo_data")
public class FooData {
   @Id
   @OneToOne
   @JoinColumn(name = "foo_id", nullable = false)
   Foo foo;



This causes a NPE here.

If I use a real Id on FooData, e.g.:
Code:
@Id
@Column(name = "foo_data_id")
Long id;

it works, and I end up with:
- foo_id in table foo as a primary key
- foo_data_id in table foo_data as a primary key
- foo_id in table foo_data not null
- foo_id in table foo_data references foo_id in table foo

This would be almost perfect except... I don't want a foo_data_id, I don't need it.

How can I express that my @OneToOne is also the @Id?

Thanks,

--
Loïc Minier


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 12, 2007 10:30 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This construct is not possible in Hibernate, what you can do is

Code:
@Entity
@Table(name = "foo")
public class Foo {
   @Id @GeneratedValue
   @Column(name = "foo_id")
   Long id;

   @OneToOne(mappedBy = "foo", cascade = CascadeType.ALL)
   FooData fooData;
...
@Entity
@Table(name = "foo_data")
public class FooData {
   @Id
   @Column(name = "foo_id", nullable = false)
   Long fooId;

   @OneToOne
   @JoinColumn(name = "foo_id", nullable = false, insertable=false, updatable=false)
   Foo foo;


It's better to use the Hibernate annotations forum for such questions, I don't follow the Hibernate Users one much :-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 12, 2007 11:50 am 
Newbie

Joined: Fri Feb 09, 2007 1:23 pm
Posts: 3
Ah this sounds like a good workaround, thanks!

Unfortunately, this results in a quite similar NPE (I think it's the same).

--
Loïc Minier


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 14, 2007 1:24 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't get a NPE when I apply my advice

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 06, 2007 10:57 am 
Newbie

Joined: Fri Feb 09, 2007 1:23 pm
Posts: 3
emmanuel wrote:
I don't get a NPE when I apply my advice


I still get the NPE; but I know what differs between your environment and mine, this is the NPE I get:
build.xml:406: java.lang.NullPointerException
at org.hibernate.tool.ant.HibernateToolTask.reportException(HibernateToolTask.java:211)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:174)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.Main.runBuild(Main.java:668)
at org.apache.tools.ant.Main.startAnt(Main.java:187)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
Caused by: java.lang.NullPointerException
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
at org.hibernate.tool.ant.ConfigurationTask.getConfiguration(ConfigurationTask.java:56)
at org.hibernate.tool.ant.HibernateToolTask.getConfiguration(HibernateToolTask.java:287)
at org.hibernate.tool.ant.Hbm2DDLExporterTask.execute(Hbm2DDLExporterTask.java:45)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:171)
... 12 more
--- Nested Exception ---
java.lang.NullPointerException
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
at org.hibernate.tool.ant.ConfigurationTask.getConfiguration(ConfigurationTask.java:56)
at org.hibernate.tool.ant.HibernateToolTask.getConfiguration(HibernateToolTask.java:287)
at org.hibernate.tool.ant.Hbm2DDLExporterTask.execute(Hbm2DDLExporterTask.java:45)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:171)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.Main.runBuild(Main.java:668)
at org.apache.tools.ant.Main.startAnt(Main.java:187)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)

With:

@Entity
@Table(name = "accounts")
public class Account {
@Id @GeneratedValue
@Column(name = "account_id")
Long id;

@OneToOne(mappedBy = "account", cascade = CascadeType.ALL)
AccountData accountData;

@Entity
@Table(name = "accounts_data")
public class AccountData {
@Id
@Column(name = "account_id", nullable = false)
Long id;

@OneToOne
@JoinColumn(name = "account_id", nullable = false, insertable = false, updatable = false)
Account account;
...


I don't get the NPE if I remove the @OneToOne from Accounts (which is mappedBy, so reversed IIUC).

Can you confirm you get the NPE with the mappedBy @OneToOne?

_________________
--
Loïc Minier


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 07, 2007 8:39 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
use branch_3_2

_________________
Emmanuel


Top
 Profile  
 
 Post subject: I get the same problem...
PostPosted: Mon Mar 26, 2007 4:52 pm 
Newbie

Joined: Mon Mar 26, 2007 4:44 pm
Posts: 1
I'm also seeing this NPE when deploying my app. I'm using Hibernate 3.2.2GA, I have three tables:

CREATE TABLE user_table
(
user_id varchar(50) not null primary key,
password varchar (32)
);

CREATE TABLE partner
(
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
partner_name VARCHAR(50) NOT NULL UNIQUE
);

CREATE TABLE user_partner_mapping
(
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_id varchar(50) not null unique,
partner INTEGER NOT NULL -- Foreign key into partner
);

ALTER TABLE user_partner_mapping ADD CONSTRAINT FK_mapping_user
FOREIGN KEY FK_mapping_user (user_id)
REFERENCES user_table (user_id)
ON DELETE RESTRICT
ON UPDATE RESTRICT;

ALTER TABLE user_partner_mapping ADD CONSTRAINT FK_mapping_partner
FOREIGN KEY FK_mapping_partner (partner)
REFERENCES partner (id)
ON DELETE RESTRICT
ON UPDATE RESTRICT;


I used Netbeans to auto-generate entity beans for these tables, and it came up with:

UserTable:

@Id
@Column(name = "user_id", nullable = false)
private String userId;

@Column(name = "password")
private String password;

@OneToOne(mappedBy = "controlPointUserId")
private ControlPoint controlPoint;

@OneToOne(cascade = CascadeType.ALL, mappedBy = "userId")
private UserPartnerMapping userPartnerMapping;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "userId")
private Collection<UserControlPointMapping> userControlPointMappingCollection;

@OneToOne(cascade = CascadeType.ALL, mappedBy = "userId")
private Customer customer;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "userId")
private Collection<GroupTable> groupTableCollection;


=============
Partner:

@Id
@Column(name = "id", nullable = false)
private Integer id;

@Column(name = "name", nullable = false)
private String name;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "partner")
private Collection<Bundle> bundleCollection;


==============
UserPartnerMapping:
@Id
@Column(name = "id", nullable = false)
private Integer id;

@Column(name = "partner", nullable = false)
private int partner;

@JoinColumn(name = "user_id", referencedColumnName = "user_id")
@OneToOne
private UserTable userId;


Any suggestions?



[list=][/list]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 26, 2007 5:09 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
use Hibernate Annotations 3.3.0

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.