-->
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.  [ 2 posts ] 
Author Message
 Post subject: Help - One To One
PostPosted: Tue Feb 11, 2014 1:24 am 
Newbie

Joined: Tue Feb 11, 2014 1:09 am
Posts: 1
I'm trying to setup a one to one relationship in the following manner.

public Class OneToOneRoot() {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;

private String name;

@OneToOne(fetch = FetchType.LAZY, mappedBy = "root", cascade = CascadeType.ALL)
private OneSharedId sharedId;

}

public Class OneSharedId() {

@GenericGenerator(name = "generator", strategy = "foreign",
parameters = @Parameter(name = "property", value = "root"))
@Id
@GeneratedValue(generator = "generator")
private long id;

private String name;

@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
private OneToOneRoot root;

}

Basically, the two classes share the same id key. Create, Update and Delete operations seem to function without issue. However, I get some weird error results for a simple criteria query.

Criteria query = session.createCriteria(OneToOneRoot.class);
OneSharedId shared = new OneSharedId();
shared.setId(2);
query.add(Restrictions.eq("sharedId",shared));
List<OneToOneRoot> matches = query.list();

I have tried many variations, with the same error results:

Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: java.sql.SQLException: No value specified for parameter 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)

I understand that using the "shareId" object as the query seems silly, considering the id is shared, but we have some existing, "too smart for its own good" code, that auto generates criteria in this manner based on meta data. Is my mapping incorrect? Is the assumption that this criteria should work flawed?


Top
 Profile  
 
 Post subject: Re: Help - One To One
PostPosted: Mon Feb 17, 2014 7:25 am 
Newbie

Joined: Wed Apr 24, 2013 7:51 am
Posts: 3
I would do something like this:

Code:
      Criteria criteria = session.createCriteria(OneToOneRoot.class);
      Criteria criteria2 = criteria.createCriteria("sharedId").add(Restrictions.eq("id", 2));
      List<OneToOneRoot> matches = criteria.list();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.