Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Tell JPA the prefered DataType

If I use JPA (EclipseLink) to create tables a String type results in a varchar2(255). How could I tell JPA (via Annotation) to create a varchar2(20) attribute.

If I have a List JPA creates a BLOB(4000) but I would like a varchar2 (my serialized object's string is short)

How is this possible? Do I have to do it by hand?

like image 373
Marcel Menz Avatar asked Nov 03 '10 15:11

Marcel Menz


People also ask

What is the difference between JPA and JPA data?

As we saw above, JPA is a standard for defining the persistence layer and Spring Data JPA is a sub-project under the Spring Framework umbrella which allows Spring applications to integrate with JPA.

Why JPA is preferred over JDBC?

The main advantage of JPA over JDBC for developers is that they can code their Java applications using object-oriented principles and best practices without having to worry about database semantics.

What is @ID in JPA?

In JPA the object id is defined through the @Id annotation and should correspond to the primary key of the object's table. An object id can either be a natural id or a generated id. A natural id is one that occurs in the object and has some meaning in the application.


2 Answers

You need to use the columnDefinition property of the @Column annotation. i.e.

@Column(columnDefinition="varchar2(20)")
like image 114
William Avatar answered Oct 19 '22 22:10

William


If I use JPA (EclipseLink) to create tables a String type results in a varchar2(255). How could I tell JPA (via Annotation) to create a varchar2(20) attribute.

Using the columnDefinition can break portability from one database to another. For a string-valued column, prefer using the length element (which defaults to 255):

@Column(length=20)
String someString;
like image 21
Pascal Thivent Avatar answered Oct 19 '22 23:10

Pascal Thivent