I am trying to add and fetching record from the table using mapping in Hibernate.
i m new in Hibernate and My project tables are complex for me to do this...
so i have taken two simple table.....
Table 1 : book PK id
name
Table 2 : author
PK id
authorname
FK bid
Now, i can add data in book table coz it is very easy...but i want to connect these two table by applying mapping ...
i have created the xml files as follows...
-----------------------------------------------------------------------------------
book.hbm.xml
<hibernate-mapping>
<class name="BookOpr.Book" table="book">
<id name="lngBookId" type="long" column="id" >
<generator class="increment"/>
</id>
<property name="strBookName">
<column name="bookname" />
</property>
</class>
</hibernate-mapping>
author.hbm.xml
<hibernate-mapping>
<class name="BookOpr.Author" table="author">
<id name="lngAuthorId" type="long" column="id" >
<generator class="increment"/>
</id>
<property name="strAuthorName">
<column name="authorname" />
</property>
<property name="lngBID" insert="false" update="false">
<column name="bid" />
</property>
<many-to-one name="Bk" column="bid"
class="BookOpr.Book" not-null="false"/>
</class>
</hibernate-mapping>
Now the beans of these two files.....
Book.java
package BookOpr;
public class Book {
private long lngBookId;
private String strBookName;
public long getLngBookId() {
return lngBookId;
}
public void setLngBookId(long lngBookId) {
this.lngBookId = lngBookId;
}
public String getStrBookName() {
return strBookName;
}
public void setStrBookName(String strBookName) {
this.strBookName = strBookName;
}
}
Author.java
package BookOpr;
public class Author {
private long lngAuthorId;
private String strAuthorName;
private long lngBID;
private Book bk;
public Author() {
}
public long getLngAuthorId() {
return lngAuthorId;
}
public void setLngBID(long lngBID) {
this.lngBID = lngBID;
}
public Book getBk() {
return bk;
}
public void setBk(Book bk) {
this.bk = bk;
}
}
I have created two Transaction files.....
BookTransaction.java
package BookOpr;
import java.io.*;
import java.lang.*;
import java.lang.Integer;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*;
import java.util.Vector;
public class BookTransaction {
public Session session = null;
public SessionFactory sf=null;
/** Creates a new instance of Transaction */
public BookTransaction() {
}
public void inserData(Book b)
{
sf = new Configuration().configure().buildSessionFactory();
session = sf.openSession();
Transaction Tr_ins = session.beginTransaction();
session.saveOrUpdate(b);
Tr_ins.commit();
session.close();
}
public List AllData()
{
System.out.println("In AllData");
sf = new Configuration().configure().buildSessionFactory();
session = sf.openSession();
Transaction Tr_ins = session.beginTransaction();
List qry=session.createQuery("from Book").list();
for(int i=0;i<qry.size();i++)
{
Book b1=(Book)qry.get(i);
System.out.println(b1.getLngBookId()+ " " + b1.getStrBookName());
}
Tr_ins.commit();
session.close();
return qry;
}
public Book searchData(int bid)
{
sf = new Configuration().configure().buildSessionFactory();
Book BData=new Book();
session = sf.openSession();
Transaction Tr_ins = session.beginTransaction();
List lstsession = session.createQuery("from Book as b where b.lngBookId = ?")
.setEntity(0,new Integer(bid)).list();
BData.setLngBookId(Long.parseLong(lstsession.get(1).toString()));
BData.setStrBookName((lstsession.get(2).toString()));
Tr_ins.commit();
session.close();
return BData;
}
}
AuthorTransaction.java
package BookOpr;
import java.io.*;
import java.lang.*;
import java.lang.Integer;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*;
import java.util.Vector;
public class AuthorTransaction {
public Session session = null;
public SessionFactory sf=null;
/** Creates a new instance of AuthorTransaction */
public AuthorTransaction() {
}
public void inserData(Author b)
{
sf = new Configuration().configure().buildSessionFactory();
session = sf.openSession();
Transaction Tr_ins = session.beginTransaction();
session.saveOrUpdate(b);
Tr_ins.commit();
session.close();
}
public Query AllData()
{
sf = new Configuration().configure().buildSessionFactory();
session = sf.openSession();
Transaction Tr_ins = session.beginTransaction();
Query qry=session.createQuery("from Author");
Tr_ins.commit();
session.close();
return qry;
}
}
now i am created two servlet in which i set the data in bean and then call the methods of transaction files.....to insert and getting data.....
In this when i insert record in Book it success fully inseted in it...
but i got the errors as follow when i insert into the author table....
Now there is no error while i insert data in Author but it ill not insert bid (reference key) value in table......
Can any one help me out.......
Thank you in advance......[/b]
|