-->
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.  [ 6 posts ] 
Author Message
 Post subject: java.sql.Date for PK!? Works well?
PostPosted: Tue Dec 16, 2003 11:13 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
I'm using Hibernate 2.0.3.
My mapping files and value objects where generated with Middlegen R3.
I'm having problems inserting an object.

-----------------
Mapping files
-----------------

OpTurno

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="pt.vo.OpTurno"
table="op_turnos"
>

<composite-id name="comp_id" class="pt.vo.OpTurnoPK">
<key-property
name="dataTurno"
column="data_turno"
type="java.sql.Date"
length="4"
/>
<!-- bi-directional many-to-one association to Turno -->
<key-many-to-one
name="turno"
class="pt.vo.Turno"
>
<column name="turno_fk" />
</key-many-to-one>
<!-- bi-directional many-to-one association to Op -->
<key-many-to-one
name="op"
class="pt.vo.Op"
>
<column name="op_fk" />
</key-many-to-one>
</composite-id>

<property
name="userId"
type="java.lang.String"
column="user_id"
not-null="true"
length="10"
/>
<property
name="dtHr"
type="java.sql.Timestamp"
column="dt_hr"
not-null="true"
length="8"
/>

<!-- associations -->
<!-- bi-directional one-to-one association to ControloVolante -->
<one-to-one
name="controloVolante"
class="pt.vo.ControloVolante"
outer-join="auto"
constrained="true"
/>
<!-- bi-directional one-to-one association to OpControloMonotorizacao -->
<one-to-one
name="opControloMonotorizacao"
class="pt.vo.OpControloMonotorizacao"
outer-join="auto"
constrained="true"
/>
<!-- bi-directional one-to-many association to OpControloInicio -->
<set
name="opControloInicios"
lazy="true"
inverse="true"
>
<key>
<column name="op_fk" />
<column name="turno_fk" />
<column name="data_turno_fk" />
</key>
<one-to-many
class="pt.vo.OpControloInicio"
/>
</set>
<!-- bi-directional one-to-many association to OpControloRejeicao -->
<set
name="opControloRejeicaos"
lazy="true"
inverse="true"
>
<key>
<column name="op_fk" />
<column name="turno_fk" />
<column name="data_turno_fk" />
</key>
<one-to-many
class="pt.vo.OpControloRejeicao"
/>
</set>
<!-- bi-directional one-to-one association to OpControloProducao -->
<one-to-one
name="opControloProducao"
class="pt.vo.OpControloProducao"
outer-join="auto"
constrained="true"
/>
<!-- bi-directional one-to-many association to OpControloParagem -->
<set
name="opControloParagems"
lazy="true"
inverse="true"
>
<key>
<column name="op_fk" />
<column name="turno_fk" />
<column name="data_turno_fk" />
</key>
<one-to-many
class="pt.vo.OpControloParagem"
/>
</set>

</class>
</hibernate-mapping>

OpControloProducao

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="pt.vo.OpControloProducao"
table="op_controlo_producao"
>

<composite-id name="comp_id" class="pt.vo.OpControloProducaoPK">
<!-- bi-directional one-to-one association to OpTurno -->
<key-many-to-one
name="opTurno"
class="pt.vo.OpTurno"
>
<column name="op_fk" />
<column name="turno_fk" />
<column name="data_turno_fk" />
</key-many-to-one>
</composite-id>

<property
name="totalProducao"
type="int"
column="total_producao"
length="4"
/>
<property
name="userId"
type="java.lang.String"
column="user_id"
not-null="true"
length="10"
/>
<property
name="dtHr"
type="java.sql.Timestamp"
column="dt_hr"
not-null="true"
length="8"
/>

<!-- associations -->
<!-- bi-directional one-to-one association to OpTurno -->
<one-to-one
name="opTurno"
class="pt.vo.OpTurno"
outer-join="auto"
/>

</class>
</hibernate-mapping>

---------
Result
---------

It keeps giving me:

Hibernate: insert into op_controlo_producao (total_producao, user_id, dt_hr, op_fk, turno_fk, data_turno_fk) values (?, ?, ?, ?, ?, ?)
WARN : SQL Error: 0, SQLState: null
ERROR: ERROR: Bad int8 external representation "2003-12-16"

ERROR: Could not synchronize database state with session
java.sql.SQLException: ERROR: Bad int8 external representation "2003-12-16"
( ... )

I'm doing "println" on all the variables and the "set's" are being done correctly.
Is it a problem to use date's as PK in Hibernate?

Please help. It's making me crazy.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 11:28 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Looks like data_turno has a wrong format in DB

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 5:13 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
epbernard wrote:
Looks like data_turno has a wrong format in DB


Nopes. "data_turno" is type DATE in DB.
I just can't find the problem.

Please help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 5:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Looks like the problem has absolutely nothing to do with Hibernate, so how about trying to get it working in JDBC first.


P.S. Using a date as a PK column is one of the most evil things you can do to your database - only do this if you know that you will never, ever need to change your data model.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 5:36 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gavin wrote:
Looks like the problem has absolutely nothing to do with Hibernate, so how about trying to get it working in JDBC first.


P.S. Using a date as a PK column is one of the most evil things you can do to your database - only do this if you know that you will never, ever need to change your data model.


Thanks Gavin.
But having the following ddl's:

CREATE TABLE OP_TURNOS (
op_fk INT8 NOT NULL
, turno_fk SMALLINT NOT NULL
, data_turno DATE NOT NULL
, user_id CHAR(10) NOT NULL
, dt_hr TIMESTAMP(10) NOT NULL
, PRIMARY KEY (op_fk, turno_fk, data_turno)
, CONSTRAINT op_turnos_op_fk FOREIGN KEY (op_fk)
REFERENCES OP (id)
, CONSTRAINT op_turnos_turnos_fk FOREIGN KEY (turno_fk)
REFERENCES TURNOS (id)
);

CREATE TABLE OP_CONTROLO_PRODUCAO (
op_fk INT8 NOT NULL
, turno_fk SMALLINT NOT NULL
, data_turno_fk DATE NOT NULL
, total_producao INT
, user_id CHAR(10) NOT NULL
, dt_hr TIMESTAMP(10) NOT NULL
, PRIMARY KEY (op_fk, turno_fk, data_turno_fk)
, CONSTRAINT op_cont_prod_op_turnos_fk FOREIGN KEY (op_fk, turno_fk, data_turno_fk)
REFERENCES OP_TURNOS (op_fk, turno_fk, data_turno)
);

It's king of weird that he's giving me that strange error.
I'm printing all of the variables right before the "session.save" and all of them are ok.
I can't understand why is he confusing "data_turno" with type "int8"!?
I think the mapping file's are explicit.

Got any clue?

PS: thanks for the reply.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 6:20 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gavin wrote:
Looks like the problem has absolutely nothing to do with Hibernate, so how about trying to get it working in JDBC first.


P.S. Using a date as a PK column is one of the most evil things you can do to your database - only do this if you know that you will never, ever need to change your data model.


I made another approach.
I've commented the "set" for the data_turno. It goes NULL now.
And a diferent error came out:


Hibernate: insert into op_controlo_producao (total_producao, user_id, dt_hr, op_fk, turno_fk, data_turno_fk) values (?, ?, ?, ?, ?, ?)
WARN : SQL Error: 0, SQLState: null
ERROR: ERROR: column "data_turno_fk" is of type 'date' but expression is of type 'integer'
You will need to rewrite or cast the expression

ERROR: Could not synchronize database state with session
java.sql.SQLException: ERROR: column "data_turno_fk" is of type 'date' but expression is of type 'integer'
You will need to rewrite or cast the expression
( ... )


Maybe this can help to better solve the problem.
Any clue on this?


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