Simple relationship problems
by boliver@lvlomas.com
I have a very simple DB structure that I can't seem to get going here with
Hibernate.
The relationship between two tables is quite simple.
Table: Team - contains the following
pkid - int
name - varchar
divisionpkid - int
Table: Division - contains the following:
pkid - int
name - varchar
A team can belong to a division, and division can have many teams. What I
want is to get all the teams from the database, and have a reference in
each team to it's division.
My hibernate hbm file looks like this:
<class name="database.HldivisionEntity" table="hldivision"
catalog="Twisters2008">
<id name="pkid">
<column name="pkid" sql-type="int" length="11" precision="0"
not-null="true"/>
</id>
<property name="name">
<column name="name" sql-type="varchar" length="20"
precision="0" not-null="false"/>
</property>
</class>
<class name="database.TeamEntity" table="team" catalog="Twisters2008">
<id name="pkid">
<column name="pkid" sql-type="int" length="11" precision="0"
not-null="true"/>
</id>
<property name="name">
<column name="name" sql-type="varchar" length="20"
precision="0" not-null="false"/>
</property>
<property name="hldivisionpkid">
<column name="hldivisionpkid" sql-type="int" length="11"
precision="0" not-null="false"/>
</property>
<one-to-one name="division" class="database.HldivisionEntity"/>
</class>
In my code, when I execute this:
List teamList = session.createQuery("from TeamEntity" ).list();
I get a List returned of the two team records in my Team table, and the
hldivisionpkid fields are set correctly, the the division object is null.
So I must be doing something wrong here. Have I missed something in my
configuration to make this relationship work correctly?
Thanks.
Bryan
----------------------------------------------------------------------------
CONFIDENTIALITY: The information in this message is legally privileged and
confidential. In the event of a transmission error and if you are not the
individual or entity mentioned above, you are hereby advised that any use,
copying or reproduction of this document is strictly forbidden. Please
advise us of this error and destroy this message.
CONFIDENTIALITÉ: L'information apparaissant dans ce message électronique
est de nature légalement privilégiée et confidentielle. Si ce message vous
est parvenu par erreur et que vous n'êtes pas le destinataire visé, vous
êtes par les présentes avisé que tout usage, copie ou distribution de ce
message est strictement interdit. Vous êtes donc prié de nous informer
immédiatement de cette erreur et de détruire ce message.
16 years, 6 months
current database time
by Walden Mathews
Hi,
"select CURRENT_TIMESTAMP" doesn't parse. Is there a canonical way of
getting the current time out of the database without referencing
anything else?
Thanks,
Walden
16 years, 7 months
Example Implementation of ResultTransformer
by Jason Ferguson
Does anyone have an example of a fairly verbose ResultTransformer
implementation that they can share?
I'm trying to implement a Nested Set transformer, based on code
provided by Christian Bauer in the project JIRA, but his
implementation won't quite work for me as it stands now (excellent,
well-documented code, though).
The ResultTransformer interface specifies two methods:
- List transformList(List collection)
- Object transformTuple(Object[] tuple, String[] alias)
I do not understand the purpose of transformList(): most
implementations are one line long, simply returning whatever was
passed to it.
I have a partial understanding of transformTuple(). The tuple
corresponds to the resultset (i.e. tuple[0] equates (bad term there, I
know) to resultSet.getXXX(0)). The alias array corresponds to any
aliases defined in the SQL.
Thanks for any help.
Jason
16 years, 7 months
mysql blob issues
by James Norman
Hello All,
I've been using hibernate with mysql for a while now with great success. I
searched everywhere and could not find a solution to this problem, so thanks
for any support you can provide.
I'm currently storing some data as a byte[] in an object and stored in the
mysql database as a blob. This was seamless with hibernate and had no
issues. I need to now deal with the data as a stream as it's bad form to
carry a 5mb byte[].
I added the methods to the object :
java.sql.Blob getBlob() and setBlob( java.jql.Blob)
and added the :
<property name="blob" type="binary" /> (tested with and without the
type="binary")
I get the following error if I don't set the blob (would be null)
And I recreate the DB each time. I get the following error when inserting
the object:
Hibernate: insert into AUDIO (bytes, name, contentType, blob) values (?, ?,
?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException:
could not insert: [com.polling.Audio]
Caused by: java.sql.SQLException: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right
syntax to use near 'blob) values
(_binary'\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0', null, null, nul' at line
1
And I get the following error if I do set the blob with a simple:
audio1.setBlob( Hibernate.createBlob( new byte[1] ) );
Exception in thread "main" java.lang.ClassCastException:
org.hibernate.lob.SerializableBlob
Environment:
Windows Xp sp2
java 1.5.0_04
Mysql 5.0.20-nt
Hibernate 3.2
Thanks again for any help.
-james
16 years, 7 months
Popular usage of Hibernate
by Sam Wun
Hi,
I am new to Hibernate.
>From googl'ing around, I found that Hibernate is designed for mapping object classes and database.
But this doesn't entirely solve my puzzle on this framework, that is what it's distinct advantage over simple usage of JDBC?
Unless Hibernate has another important/popular usage rather than only dealing with database, I would think this is may be the only reason why Hibernate getting more popularity.
Can anyone tell me?
Thanks
Sam
____________________________________________________________________________________
You rock. That's why Blockbuster's offering you one month of Blockbuster Total Access, No Cost.
http://tc.deals.yahoo.com/tc/blockbuster/text5.com
16 years, 7 months
Building a smart cascade deletion routine
by Josh Long
Hello all,
I'm looking for a way to guarantee a cascade deletion. I am generating
everything from an ER tool and thus all associations are navigable
from either side of the relationship. Does any one have any experience
crawling such an object graph to orphan off any foreign keys and then
ensure the correct deletion of a record?
I know that I can get at the runtime meta data by casting to
HibernateProxy. Is there any case where I wouldn't be able to cast to
HibernateProxy? I understand that I might end up loading every lazy
object in the system if I'm not careful. Is there a way to seize onto
the runtime information and gleem which side oft he relationship is
the owning side, for example. Does it make a difference since no
associations are set to be cascade orphan delete?
I am working with JPA on JBoss,so I have access tot he underlying
Hibernate engine if need be.
Any input / gotchas would be appreciated!
Thanks,
--
Joshua Long
Sun Certified Java Programmer
http://www.joshlong.com/
16 years, 8 months
constraint name for a primary key
by thalles
I need a help,
how can i to define the constraint name for a primary key in the
hibernate-mapping (to generate database objects with SchemaExport)?
tanks.
___________________________________________________________________________________
Para fazer uma ligação DDD pra perto ou pra longe, faz um 21. A Embratel tem
tarifas muito baratas esperando por você. Aproveite!
16 years, 8 months
TimestampType converts to localtime?
by Robert Nicholson
So,
if the default timezone for my vm is say CST
and I parse a date that's in GMT using a SimpleDateFormat ('z') and
get a Date object. That gives me a Date ie. timestamp.
If I want to store that via TimestampType to the database will it
store the time in local/default timezone and not GMT?
Do I have to write my own CustomType if I do not wish it to store the
time as a local time?
If I want to store the time as UTC do I have to use a Calendar in the
user defined type that converts the Date to UTC?
16 years, 8 months