-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate problem with Fixed Length CHAR
PostPosted: Sun Apr 27, 2008 12:20 am 
Newbie

Joined: Sat Apr 26, 2008 10:45 pm
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1

Mapping documents:
Item3.hbm.xml
........
.......
<set name="bid3s" inverse="true">
<key>
<column name="ITEM_ID" length="10" not-null="true" />
</key>
<one-to-many class="com.hibernatetest.Bid3" />
</set>
..
..

Bid3.hbm.xml
<many-to-one name="item3" class="com.hibernatetest.Item3" fetch="select">
<column name="ITEM_ID" length="10" not-null="true" />
</many-to-one>



Code between sessionFactory.openSession() and session.close():

public Items2 findById(java.lang.String id) {
log.debug("getting Items2 instance with id: " + id);
try {
Items2 instance = (Items2) getSession().get(
"com.hibernatetest.Items2", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}


Full stack trace of any exception that occurs:No Exception

Name and version of the database you are using:
Derby 10.2.2.0

The generated SQL (show_sql=true):
select items2x0_.ID as ID1_0_, items2x0_.NAME as NAME1_0_ from CLASSICCARS.ITEMS2 items2x0_ where items2x0_.ID=?
select bids2s0_.ITEM2_ID as ITEM2_1_, bids2s0_.ID as ID1_, bids2s0_.ID as ID0_0_, bids2s0_.ITEM2_ID as ITEM2_0_0_, bids2s0_.BIDDER as BIDDER0_0_ from CLASSICCARS.BIDS2 bids2s0_ where bids2s0_.ITEM2_ID=?


Debug level Hibernate log excerpt:

I am trying to do a simple One To Many mapping with CHAR(10).

I have two tables as follows:
create table "CLASSICCARS"."ITEM3"(
"ITEM" CHAR(10) not null,
"ITEM_DESC" CHAR(30) not null,
constraint "SQL080425111250090" primary key ("ITEM")
);
--------------
Data in Table
--------------

ITEM ITEM_DESC
ONE laptop

create table "CLASSICCARS"."BID3"(
"BIDID" CHAR(10) not null,
"BID_DESCR" CHAR(30) not null,
"ITEM_ID" CHAR(10) not null,
constraint "SQL080425111415120" primary key ("BIDID")
);

alter table "CLASSICCARS"."BID3"
add constraint "BID3_TO_ITEM3"
foreign key ("ITEM_ID")
references "CLASSICCARS"."ITEM3"("ITEM");
-------------
Data in Bid Table
----------------------
BIDID BID_DESCR ITEM_ID
1 XXX ONE
2 XXX ONE

The classes are
Item3.java
package com.hibernatetest;

import java.util.HashSet;
import java.util.Set;

/**
* Item3 entity.
*
* @author MyEclipse Persistence Tools
*/

public class Item3 implements java.io.Serializable {

// Fields

private String item;
private String itemDesc;
private Set bid3s = new HashSet(0);

// Constructors

/** default constructor */
public Item3() {
}

/** minimal constructor */
public Item3(String item, String itemDesc) {
this.item = item;
this.itemDesc = itemDesc;
}

/** full constructor */
public Item3(String item, String itemDesc, Set bid3s) {
this.item = item;
this.itemDesc = itemDesc;
this.bid3s = bid3s;
}

// Property accessors

public String getItem() {
return this.item;
}

public void setItem(String item) {
this.item = item;
}

public String getItemDesc() {
return this.itemDesc;
}

public void setItemDesc(String itemDesc) {
this.itemDesc = itemDesc;
}

public Set getBid3s() {
return this.bid3s;
}

public void setBid3s(Set bid3s) {
this.bid3s = bid3s;
}

}

Bid3.java
package com.hibernatetest;

/**
* Bid3 entity.
*
* @author MyEclipse Persistence Tools
*/

public class Bid3 implements java.io.Serializable {

// Fields

private String bidid;
private Item3 item3;
private String bidDescr;

// Constructors

/** default constructor */
public Bid3() {
}

/** full constructor */
public Bid3(String bidid, Item3 item3, String bidDescr) {
this.bidid = bidid;
this.item3 = item3;
this.bidDescr = bidDescr;
}

// Property accessors

public String getBidid() {
return this.bidid;
}

public void setBidid(String bidid) {
this.bidid = bidid;
}

public Item3 getItem3() {
return this.item3;
}

public void setItem3(Item3 item3) {
this.item3 = item3;
}

public String getBidDescr() {
return this.bidDescr;
}

public void setBidDescr(String bidDescr) {
this.bidDescr = bidDescr;
}

}
---------------------------------------------------
The FindbyId method in DAO is as follows:
-----------------------------------------------------
public Item3 findById(java.lang.String id) {
log.debug("getting Item3 instance with id: " + id);
try {
Item3 instance = (Item3) getSession().get(
"com.hibernatetest.Item3", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}

------------------------------------------------------------------
Bassed on the above information the following code runs fine
Please note that, the input is padded by spaces

-------------------------------------------------------------------
Item3DAO dao = new Item3DAO();
Item3 item3 = dao.findById("ONE "); //ONE followed by 7 spaces

System.out.println("item3= " + item3);
System.out.println("Bid s= " + item3.getBid3s());
-----------
Output
-----------
Hibernate: select item3x0_.ITEM as ITEM1_0_, item3x0_.ITEM_DESC as ITEM2_1_0_ from CLASSICCARS.ITEM3 item3x0_ where item3x0_.ITEM=?
item3= com.hibernatetest.Item3@d16fc1
Hibernate: select bid3s0_.ITEM_ID as ITEM2_1_, bid3s0_.BIDID as BIDID1_, bid3s0_.BIDID as BIDID0_0_, bid3s0_.ITEM_ID as ITEM2_0_0_, bid3s0_.BID_DESCR as BID3_0_0_ from CLASSICCARS.BID3 bid3s0_ where bid3s0_.ITEM_ID=?
Bid s= [com.hibernatetest.Bid3@1d88db7, com.hibernatetest.Bid3@908881]

-------------------------------------
But the following does not work
------------------------------------
Item3DAO dao = new Item3DAO();
Item3 item3 = dao.findById("ONE"); // No spaces after "ONE"

System.out.println("item3= " + item3);
System.out.println("Bid s= " + item3.getBid3s());

----------
Output
----------
Hibernate: select item3x0_.ITEM as ITEM1_0_, item3x0_.ITEM_DESC as ITEM2_1_0_ from CLASSICCARS.ITEM3 item3x0_ where item3x0_.ITEM=?
item3= com.hibernatetest.Item3@d16fc1
Hibernate: select bid3s0_.ITEM_ID as ITEM2_1_, bid3s0_.BIDID as BIDID1_, bid3s0_.BIDID as BIDID0_0_, bid3s0_.ITEM_ID as ITEM2_0_0_, bid3s0_.BID_DESCR as BID3_0_0_ from CLASSICCARS.BID3 bid3s0_ where bid3s0_.ITEM_ID=?
Bid s= []

------------------------------------
Summary
-------------------------------------
You can see that the Hibernate query is same but in the second case it is not fetching any records. I know that it is happening for fixed length characters in Derby data base.

Does any one know how to fix this?
Also can someone tell how to solve this with JPA annotations?





Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


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

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.