Spring roo entities - storing long strings - java

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.

Related

How to make Spring Data JPA by default naming from camelCase to snake_case unless explicitly specified?

By default, my Spring Boot project had JPA mapping entity properties to table columns by transforming the names from camelCase to snake_case (e.g. from fooBar to foo_bar).
I'm working on an existing DB, where the entity property names were kept as is for the table column names (e.g. from fooBar to fooBar), so I tried to use the #Column annotation to explicitly specify the name:
#Column(name = "fooBar")
private String fooBar;
This wasn't working. I followed the advice given here:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
My #Column is now successfully taken into account, but I'm losing the camelCase to snake_case implicit transformation on the properties that don't have the #Column the annotation.
Any idea how to transform from camelCase to snake_case by default, unless explicitly specified in the #Column annotation?
Somehow the default JPA behavior of converting to snake case was not working when I injected my custom spring datasource beans for my H2 and postgres repositories. I wanted to convert camel case to snake case by default without explicitly annotating columns while saving objects to DB. Hence adding below property in application.properties worked for me:
spring.jpa.properties.hibernate.physical_naming_strategy: org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy
Note: I am using spring boot version 2.6.2
Automatically transforming property-Names using camelCase (as is the naming convention in Java) to SNAKE_CASE seems to be no longer supported as of Hibernate 5. However, you can write your own Implementation of a NamingStrategy, doing exactly that.
Please check this Stackoverflow-Answer to see how it is done.

How to insert an object(more than 10 properties) into mysql via mybatis based on annotation without list all properties

I want to insert an Object with more than 10 properties into mysql via mybatis based on annotation. But I must list all properties, it's too inconvenient. I want to know is there some ways to insert an object easily without list all properties via mybatis. Here is my snippet. Thanks a lot.
#Insert("insert into poi_shop(name,brand,tags,status,phone,mobile,business_time,address,city,lng,lat,business_type,attribute_json) values(#{name},#{brand},#{tags},#{status},#{phone},#{mobile},#{business_time},#{address},#{city},#{lng},#{lat},#{business_type},#{attribute_json})")
#Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
public Long insertPoiInfo(PoiBo poiBo);
It is not possible in MyBatis (at least version 3) out of the box.
MyBatis has auto mapping when reading data from database but doesn't have option to automatically map fields on insertion.
The reason for this is that MyBatis is very SQL centric that is you need to write SQL manually. It is possible to have implicit fields in SQL select statement (select * from table) so there is automatic mapping to POJO in this case but it is not possible to have implicit fields in update or insert hence no auto-mapping.
MyBatis can be extended of cause. For example you can use #InsertProvider/#UpdateProvider with the sql generator that generates sql using reflection to get object fields.

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

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

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.

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