-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to set the length of a UserType column with XDoclet?
PostPosted: Tue Oct 28, 2003 3:18 pm 
Beginner
Beginner

Joined: Wed Oct 15, 2003 3:08 pm
Posts: 32
I have an object which contains a typesafe enum value that I would like to persist to the database. After searching the forums, using UserType appeared to be the best solution as it avoids putting mapping code in the domain layer.

The UserType gets and sets a String that I would like to be a fixed-size CHAR column. The problem is that the exported schema appears to set the size of the CHAR to 1. Although not particularly logical, I did try setting the length property on the domain object to 20. I didn't think that would work because you can have user types that are composites of several columns.

So all that said, is there a way to explicitly set the length of a UserType column using XDoclet tags?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 3:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Use the @hibernate.column tag and specify the length


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 3:39 pm 
Beginner
Beginner

Joined: Wed Oct 15, 2003 3:08 pm
Posts: 32
Thanks, I will give that a shot. When I searched http://xdoclet.sourceforge.net/tags/hibernate-tags.html I did not find that tag. But after you suggested @hibernate.column I did a google search and found: http://www.coyotesong.com/xdoclet/tags/ ... ty%20(0..1). I guess the docs are slightly out of sync. Thanks again for the help!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 4:36 pm 
Beginner
Beginner

Joined: Wed Oct 15, 2003 3:08 pm
Posts: 32
OK, I tried the @hibernate.column tag to no avail. Maybe I am using a version of xdoclet that is too old? I am using the version that comes with maven (1.2b2).

Anyway, I did try putting the length field on the @hibernate.properties tag again like so:

Code:
@hibernate.property column="state" type="XXXStateType" length="20"


This did seem to modify the generated .hbm.xml file as shown below:

Code:
   
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping.dtd">

<hibernate-mapping>

   <class
      name="XXXDomainObject"
      table="xxx_domain_object"
   >

       <id
         name="id"
         column="id"
      >
         <generator class="uuid.hex">
         </generator>
       </id>

        <property
          name="displayName"
            type="java.lang.String"
            column="display_name"
            not-null="false"
         unique="false"
      />

        <property
          name="state"
            type="XXXStateType"
            column="state"
         length="20"
            not-null="false"
         unique="false"
      />
   </class>

</hibernate-mapping>


Note that the length attribute on the property element is set to 20. However, when I try to export the schema, this is produced:

Code:
drop table xxx_domain_object
create table xxx_domain_object (
   id VARCHAR(255) not null,
   display_name VARCHAR(255),
   state CHAR(1),
   primary key (id)
)


It still appears to want to set the state field to "CHAR(1)". Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 5:12 pm 
Beginner
Beginner

Joined: Wed Oct 15, 2003 3:08 pm
Posts: 32
I tried using VARCHAR instead of CHAR. That actually worked just fine and generated the correct DDL. Since I am already using VARCHAR in the table, I will probably just go this route. I still don't understand why the CHAR column was not letting me set its length though.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 28, 2003 5:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It is the jdbc-type attribute you are looking for.

Anyway, the "char(1) " thing tells me that your UserType is defined as being of SQL Type.CHAR, which our Dialects may to CHAR(1).


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 1:46 pm 
Newbie

Joined: Fri Mar 05, 2004 11:02 am
Posts: 3
Hello all,

The discussion interests me. My problem is to convince a good old DBA to use a VARCHAR(3) in place of a CHAR(3) for a field which is ALWAYS filled (typical for enumerated types)... It is somewhat difficult.

Well for now, at least I am able to create a CHAR(1) ;-)

I tried the XDoclet @hibernate.colum tag, but it does not generate my field in the mapping file. I then tried the @hibernate.property tag, then I had the field generated, but only the length parameter exists, not the sql-type or anything around the SQL type...

Any idea on how to force Hibernate and the round trip tools to generate a CHAR(n) in the DDL statement ?

Thanks

_________________
Damien Boucquey


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 17, 2004 6:11 am 
Newbie

Joined: Fri Mar 05, 2004 11:02 am
Posts: 3
I found it out :)

The XDoclet tags in the java code is as follows:

Code:
/**
*    @hibernate.property
*     name="title"
*     update="true"
*     insert="true"
*     @hibernate.column
*      name="TITLE"
*      sql-type="CHAR(3)"
*/   


The mapping file generated looks like:

Code:
...
         <property
            name="title"
            type="java.lang.String"
            update="true"
            insert="true"
        >
            <column
                name="TITLE"
                sql-type="CHAR(3)"
            />
        </property>
...


This leads to the following DDL:

Code:
create table TEST (
...
   TITLE CHAR(3),
...
);


However, the column element is documented in XDoclet as accepting the length attribute, but putting it like this
Code:
<column name="TITLE" sql-type="CHAR" length="3"/>


lead to a CHAR(1) again...

_________________
Damien Boucquey


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