Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

beginner one-to-one problem

 
YanJun Tong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,I have two tables called author and booksinfo.my xml files are as follows:
author.hbm.xml
......
<id name="authorID"
column="AuthorID" type="string" length="10">
<generator class="assigned">
</generator>
</id>
<property name="usrname">
<column name="Name" not-null="false" sql-type="varchar(30)"/>
</property>
</class>
bookinfo.hbm.xml
......
<class name="demo.bookinfo" table="booksinfo">
<id column="authorID" name="authorid" type="string" length="10">
<generator class="assigned">
</generator>
</id>
<one-to-one cascade="all" class="demo.author" name="author" constrained="true"/>
<property name="isbn">
<column name="isbn" not-null="false" sql-type="varchar(10)"/>
</property>

<property name="price">
<column name="Price" not-null=" true" sql-type="varchar(10)"/>
</property>
......
my test file is:
.....
SessionFactory sessions = conf.buildSessionFactory();
Session s = sessions.openSession();
Transaction tx = s.beginTransaction();
author a = new author();
a.setAuthorID("1");

a.setusrname("liuyan");
//s.save(a);
bookinfo b = new bookinfo();
b.setAuthorid("1");
b.setIsbn("1");
b.setPrice("4");
b.setAuthor(a);
s.save(b);
tx.commit();
.....
When I try to run it it gives me an HibernateException: SQL insert, update or delete failed (row not found)error.
Could anybody tell me how to resolve this problem? thanks a million.
By the way my bookinfo.java is as follows:
package demo;
public class bookinfo {
private author author;
private String isbn;
private String price;
private String authorid;
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public author getAuthor() {
return author;
}
public void setAuthor(author author) {
this.author = author;
}
public String getAuthorid() {
return authorid;
}
public void setAuthorid(String authorid) {
this.authorid = authorid;
}}
my author.java is as follows:
package demo;
import java.io.Serializable;
public class author implements java.io.Serializable{
private String usrname;
private String id;
public String getAuthorID(){
return id;
}
public void setAuthorID(String authorID) {
id=authorID;
}
public String getusrname() {
return usrname;
}
public void setusrname(String usrname) {
this.usrname = usrname;
}}
I do not think I missed any row in my codes :'(
Please help me.
 
YanJun Tong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anybody give me a entire one-to-one example? thanks a million!
 
Allex last
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.hibernate.org/78.html view link to xdocklet, maby another good to , i dont know
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know Hibernate yet, but I know Object Relational Bridge, and the one-one relationship there was backwards, I thought the "Parent" would have the relationship information, but it appears that the "Child" had to have it.

Maybe your mapping is backwards. Have you tried putting you "<one-to-one" tag in the other table definition?

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic