You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Emmanuel Dupont <em...@jwaretechnologies.com> on 2003/12/01 10:29:58 UTC
Field length control
All,
What is the best solution to control the field length of a JSP application
with OJB.
The repository. Xml doesn't contain any information about the database field
length.
Anyway if it would do it, I would have the same trouble. All the jdo
attribute I use are String, etc.and in database I have Varchar(20), Varchar
(4) etc..
Any help, please...
Re: Field length control
Posted by Gus Heck <gu...@olin.edu>.
Christian Pesch wrote:
> Gus Heck wrote:
>
>> [..]
>
>
>> /**
>> * The actual data that needs to be persisted for a location.
>> *
>> * @ojb.class * @jdo.persistence-capable
>> */
>> public class LocationBase implements Location {
>> /**
>> * @ojb.field primarykey="true"
>> */
>> Integer id;
>> /**
>> * @ojb.field length="40"
>> */
>> String country;
>
>
>
> If you want to write a setter method for this
> field:
>
> public void setCountry(String newCountry) {
> if(newCountry != null && newCountry.length() > 40)
> a) newCountry = newCountry.substring(0, 40);
> b) throw new StringTooLongException("Length of country
> has to be less or equals than 40 characters");
> this.country = newCountry;
> }
I havn't yet tried to do this... But I am aware that it is a problem
that I will need to solve. I currently am using struts, and setting the
max length on the form to be the same as in the database, but when I get
around to it I will try to take out the form side restrictions and do
something more like what you are suggesting here, because it is a
violation of DRY :) It also violates MVC to depend on setting the field
size in the view as I am because the view is now aware of the feild size
parameters of the model/database, and thus the model and the view become
linked.
>
> Regardless, if you want to reduce the size of the
> string or let the caller do it: you replicate the
> size of the string (here: 40) in the code.
>
>>
>> This puts the information in one place. which is very nice and easy
>> to maintain.
>
>
> How do you solve the problem above?
>
I havn't yet. Post your solution if you find it, I'll post mine when I
find one :)
-Gus
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: Field length control
Posted by Christian Pesch <ch...@coremedia.com>.
Gus Heck wrote:
> [..]
> /**
> * The actual data that needs to be persisted for a location.
> *
> * @ojb.class * @jdo.persistence-capable
> */
> public class LocationBase implements Location {
> /**
> * @ojb.field primarykey="true"
> */
> Integer id;
> /**
> * @ojb.field length="40"
> */
> String country;
If you want to write a setter method for this
field:
public void setCountry(String newCountry) {
if(newCountry != null && newCountry.length() > 40)
a) newCountry = newCountry.substring(0, 40);
b) throw new StringTooLongException("Length of country
has to be less or equals than 40 characters");
this.country = newCountry;
}
Regardless, if you want to reduce the size of the
string or let the caller do it: you replicate the
size of the string (here: 40) in the code.
>
> This puts the information in one place. which is very nice and easy to
> maintain.
How do you solve the problem above?
--
Christian
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: Field length control
Posted by Gus Heck <gu...@olin.edu>.
What I seem to have found regarding this is that OJB wants you to tell
it what the field length is in the database, and this is not related to
any of the JDO api or the .jdo files. If you want to determine the field
length outside of your database, you need to use Torque or some other
tool to push the database schema into the db. Currently I am doing this
by using Xdoclet to generate a torque style xml schema for my database,
though you can certainly code one by hand too. The JDO stuff is not
related, and does not change OJB's understanding of what is in the
database (which sits behind the JDO API). The JDO API wraps the existing
OJB mechanisms.
You may wish to check out this link which will point you at a variety of
options for working with OJB including xdoclet:
http://db.apache.org/ojb/howto-build-mappings.html
The xdoclet task for ojb is in the contrib directory of the ojb source
download. I have heard that it will eventually become part of the
xdoclet distro, but this has not yet happend as far as I know.
The reason I chose xdoclet is cause It will generate, the JDO
descriptors, the torque schema and the repository.xml from a class that
looks like this:
/**
* The actual data that needs to be persisted for a location.
*
* @ojb.class
* @jdo.persistence-capable
*/
public class LocationBase implements Location {
/**
* @ojb.field primarykey="true"
*/
Integer id;
/**
* @ojb.field length="40"
*/
String country;
/**
* @ojb.field length="60"
*/
String stateOrRegion;
/**
* @ojb.field length="60"
*/
String subRegion;
/**
* @ojb.field length="60"
*/
String city;
(Rest of class is irrelevant)
in my Ant build I use a target like:
<!-- ==================== Xdoclet OJB stuff
================================== -->
<target name="repository-files" depends="init">
<taskdef name="ojbdoclet"
classname="xdoclet.modules.ojb.OjbDocletTask">
<classpath refid="compile.classpath"/>
</taskdef>
<ojbdoclet destdir="${build.home}/WEB-INF/classes/">
<fileset dir="./src" excludes=".nb*"/>
<ojbrepository destinationFile="repository_user.xml"/>
<torqueschema databaseName="fdbtest2"
destinationFile="project_schema.xml"/>
</ojbdoclet>
</target>
I get an entry for Country that looks like this in my
repository_user.xml file:
<field-descriptor
name="country"
column="country"
jdbc-type="VARCHAR"
length="40"
>
which matches this entry in my project_schema.xml:
<column name="country"
javaName="country"
type="VARCHAR"
size="40"
/>
This puts the information in one place. which is very nice and easy to
maintain.
-Gus
Emmanuel Dupont wrote:
>All,
>
>
>
>What is the best solution to control the field length of a JSP application
>with OJB.
>
>
>
>The repository. Xml doesn't contain any information about the database field
>length.
>
>
>
>Anyway if it would do it, I would have the same trouble. All the jdo
>attribute I use are String, etc.and in database I have Varchar(20), Varchar
>(4) etc..
>
>
>
>
>
>Any help, please...
>
>
>
>
>
>
>
>
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org
Re: Field length control
Posted by Thomas Dudziak <to...@first.gmd.de>.
On Mon, 1 Dec 2003, Emmanuel Dupont wrote:
> All,
>
> What is the best solution to control the field length of a JSP application
> with OJB.
>
> The repository. Xml doesn't contain any information about the database field
> length.
>
> Anyway if it would do it, I would have the same trouble. All the jdo
> attribute I use are String, etc.and in database I have Varchar(20), Varchar
> (4) etc..
>
> Any help, please...
There is an attribute "length" for the field-descriptors which should help
you (there are also "precision" and "scale" attributes for DECIMAL/NUMERIC
fields).
I have zero knowledge about JDO, but if you can use Torque to initialize
your database then you can also use the "size" attribute of the column
tags.
Tom
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org