Java EE, Entity Bean and Long String attribute (mysql text). How? - java

How can I declare within an Entity Bean a Long Text attribute?
For example: String description
I would like it to be more than 255 varchar, and maybe mapped on MySQL as TEXT.
Thanks
Solved:
JPA: how do I persist a String into a database field, type MYSQL Text
Using #Lob annotation

Just define the corresponding column in database as TEXT, and JPA should happily map the column to your attribute.

Related

JPA Entities from Tables turns int primary key to String

I use Eclipse --> JPA Entities from Tables get generate entities from mysql database. All the tables' primary keys -- ids are int(11) AI PK. So instead of int or long, i am getting String for all the keys. What did i do wrong?
Thanks!
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="LOCATION_ID")
private String locationId;
Eclipse is not correctly parsing your primary key's data type (int(11)). As a result, Eclipse is unable to resolve the appropriate data type to Java type mapping. Because that mapping is missing, Eclipse defaults the datatype for primary key attributes to String. You could remove the display width attribute from your datatype (i.e. the (11)) and Eclipse should be able to more appropriately map the data type.
Just found out when generating the entity, click on the id and eclipselink will allow you to select the mapping type. Choose int instead of String will do the trick.

Get physical column value with entity property value using hibernate

I have a table T with columns defined as usual.
#Entity
#Table(name="T")
public class T {
#Column(name="test_id")
private Long testId;
}
Given entity property "testId", I want to get corresponding DB column name (i.e. "test_id"). How could it be achieved?
Edit 1:
I want to keep this column at separate location with actual DB column name (test_id) than testId. I fetched these values from DB using HQL which have key as entity name (i.e. testId) and I want actual column name in DB.
If I understood your requirement correctly, you want to use HQL while having a consistent name for both DB column and the entity field, like this:
SELECT t.test_id FROM Test t
instead of
SELECT t.testId FROM Test t
There is only one way to do that - renaming the field to test_id. HQL works on entities, not on DB tables, so you must use proper field names in the query.
Since test_id contradicts the usual Java coding conventions, I would advise against it.
EDIT: Getting the annotation attribute value with reflection would work along this outline:
Field field = MyEntity.class.getDeclaredField("testId");
Column a = field.getAnnotation(Column.class);
String columnName = a.name();
I would try to avoid this by any means, but if you're really sure you'll need it, use:
Configuration configuration = sessionFactory.getConfiguration();
PersistentClass persistentClass = configuration
.getClassMapping(T.class.getName());
String columnName = ((Column) persistentClass.getProperty("testId")
.getColumnIterator().next()).getName();
See also Get table column names in Hibernate

Custom defined column name for JSONObject key

I have a JSONObject in which I am putting keys and values from columns in my database. I want the key name to show up as a custom defined name, while I have it equivalent to the column name in the DB. How do I do this?
For eg, I have -
myObj.put(resultSet.getString(personId), personCarInfo);
While I want it to be -
myObj.put("personId", personCarInfo);
In JDBC, you can query the name of a column using the ResultSets meta data:
String columnName = resultSet.getMetaData().getColumnName(columnIndex);
String columnValue = resultSet.getString(columnIndex); // assuming, its a string
myObj.put(columnName, columnValue);
If I understood you correctly, you want to have mapping between column names and your custom defined names.
You could create simple one-directional mapping using statically initialized hashMap or by using enums.
If you want bi-directional mapping (i.e. be able to restore your table column name string from custom "personId" string) you could use Guava BiMap, for example.
Hope that helps.

Spring roo entities - storing long strings

I have to store long strings in MySQL database using spring roo. I assumed that "field string" command generates field with size 255 which is too small. I prefer to not use blob. What should I do?
If you create the field using a command like field string --fieldName field1 --sizeMax 500 then Roo will annotate the field with #Size(max = 500) and it works for me if I let Hibernate to create the database schema.
(--sizeMax is an optional parameter, you can display all optional parameters after you defined all mandatory ones with -- and hitting TAB)
Another solution is to add manually the JPA annotation on the field: #Column(length=500).
Or if you don't generate the database schema but create it by hand then you can define your column as you like.

Oracle Char type and Hibernate

I have a oracle table which contains char(n) type for several columns. I use hibernate tools to create entities objets and this tool map char type in String.
But when I deploy my application, I get an error because Hibernate wait a varchar2 type and not a char type:
Wrong column type in ARBOR.CMF for column CHG_WHO. Found: char, expected: varchar2(30 char)
What kind of java type must I use to map char(n) type in an entity ?
Thanks.
There's some useful information here on this blog entry.
Essentially you need to make your hibernate configuration more specific, as it's assuming a default String mapping to varchar2(30).
You can try using the database-agnostic annotation of #Column(length=N) (where N is the actual length of your column in the database). If that doesn't work, try using the #Column(columnDefinition = "char") approach.
Doesn't work - fails Hibernate schema validation:
#Column(name="ENABLED_FLAG", length=1)
private String enabledFlag;
Does work, since the columnDefinition attribute tells Hibernate not to default to VARCHAR2 as the column type, and to use CHAR instead:
#Column(name="ENABLED_FLAG", length=1, columnDefinition="CHAR")
private String enabledFlag;
The column in the database is defined as:
ENABLED_FLAG CHAR(1 BYTE)

Categories