hibernate column length annotation

B

Biagio

Hi to all,

I using Hibernate and I have this annotation

@Entity
@Table(name = "anagrafica", catalog = "", schema = "")

@Column(name = "codanag", nullable = false, length = 6)
private Integer codanag;


How to retrieve the lenght? Can I have a sample please?

Thanks

Biagio
 
A

Arne Vajhøj

I using Hibernate and I have this annotation

@Entity
@Table(name = "anagrafica", catalog = "", schema = "")

@Column(name = "codanag", nullable = false, length = 6)
private Integer codanag;


How to retrieve the lenght? Can I have a sample please?

Get a reference to a java.lang.reflect.Field object for
it, call getAnnotation(Column.class) on it and call
the length method.

Arne
 
B

Biagio

Get a reference to a java.lang.reflect.Field object for
it, call getAnnotation(Column.class) on it and call
the length method.

Arne

Many thanks ...... is perfect !!!!

Biagio
 
L

Lew

Many thanks ...... is perfect !!!!

In future, do not multipost. It's very bad.

I am curious about the context of your question. It is very unusual to need a
length of a database column, at least of an Integer column, from the object
viewpoint. What is the scenario that makes this necessary?
 
B

Biagio

In future, do not multipost.  It's very bad.

I am curious about the context of your question.  It is very unusual to need a
length of a database column, at least of an Integer column, from the object
viewpoint.  What is the scenario that makes this necessary?

I'm sorry for the multipost.

I use NetBeans and JPA annotation (hibernate). To limit a number of
caracters in a JTextField I use this code:

this.fld_cap.setDocument(new JTextFieldLimit(5));

Using a for loop I can write less code.

Thanks

Biagio
 
L

Lew

(don't quote sigs)
I use NetBeans and JPA annotation (hibernate). To limit a number of
caracters in a JTextField I use this code:

this.fld_cap.setDocument(new JTextFieldLimit(5));

Using a for loop I can write less code.

That's rather clever, although I can see limitations. For one thing, the
concept of "number of characters" is rather irrelevant for integers. That
normally will not substitute satisfactorily for a range check. Ergo you need
a range check anyway, ergo the limitation on widget capacity is redundant.
Elimination of the character-length loop takes even less code.

However I also can imagine rare use cases where this technique can be helpful.
I say rare because nearly all the time this type of check needs to be an
explicit validation in the model, not the view.

I am somewhat biased by working on projects where GUI screens are not the only
input channel; most systems I've worked on have EDI channels. To keep range
checks and other validations consistent irrespective of external interface,
they must be enforced in the model. Futhermore, in most systems the lengths
in the database table columns are outer bounds; for each, the actual business
rule specifies a more restricted domain of values. So in most scenarios it
doesn't really make sense for the database's physical structure to be the
arbiter of logical requirements.

But as I said, it's a neat trick and one that I now add to my arsenal.
 
A

Arne Vajhøj

That's rather clever, although I can see limitations. For one thing, the
concept of "number of characters" is rather irrelevant for integers.
That normally will not substitute satisfactorily for a range check. Ergo
you need a range check anyway, ergo the limitation on widget capacity is
redundant. Elimination of the character-length loop takes even less code.

However I also can imagine rare use cases where this technique can be
helpful. I say rare because nearly all the time this type of check needs
to be an explicit validation in the model, not the view.

I am somewhat biased by working on projects where GUI screens are not
the only input channel; most systems I've worked on have EDI channels.
To keep range checks and other validations consistent irrespective of
external interface, they must be enforced in the model. Futhermore, in
most systems the lengths in the database table columns are outer bounds;
for each, the actual business rule specifies a more restricted domain of
values. So in most scenarios it doesn't really make sense for the
database's physical structure to be the arbiter of logical requirements.

But as I said, it's a neat trick and one that I now add to my arsenal.

MySQL supports something similar at the database level:
http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html

<quote>
M indicates the maximum display width for integer types. The maximum
legal display width is 255. Display width is unrelated to the range of
values a type can contain, as described in Section 10.2, “Numeric Typesâ€.
</quote>

Arne
 
B

Biagio

(don't quote sigs)




That's rather clever, although I can see limitations.  For one thing, the
concept of "number of characters" is rather irrelevant for integers.  That
normally will not substitute satisfactorily for a range check.  Ergo you need
a range check anyway, ergo the limitation on widget capacity is redundant..
Elimination of the character-length loop takes even less code.

However I also can imagine rare use cases where this technique can be helpful.
  I say rare because nearly all the time this type of check needs to be an
explicit validation in the model, not the view.

I am somewhat biased by working on projects where GUI screens are not the only
input channel; most systems I've worked on have EDI channels.  To keep range
checks and other validations consistent irrespective of external interface,
they must be enforced in the model.  Futhermore, in most systems the lengths
in the database table columns are outer bounds; for each, the actual business
rule specifies a more restricted domain of values.  So in most scenarios it
doesn't really make sense for the database's physical structure to be the
arbiter of logical requirements.

But as I said, it's a neat trick and one that I now add to my arsenal.

Everything you write is right, the number of characters is not
important to an integer, but my example was only to understand the
concept.
This code is used mainly to limit the number of characters for String
variables in JTextField.
The goal is to write less code possible, making it very standard.
Less code to write = less errors and less time spent in tedious tasks.

Thanks

Biagio
 
T

Travers Naran

Everything you write is right, the number of characters is not
important to an integer, but my example was only to understand the
concept.
This code is used mainly to limit the number of characters for String
variables in JTextField.
The goal is to write less code possible, making it very standard.
Less code to write = less errors and less time spent in tedious tasks.

If you are using JSF 2, have you considered Hibernate Validator?

http://docs.jboss.org/hibernate/sta...en-US/html_single/#section-presentation-layer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top