Spring CrudRepository not saving more than 32,000 characters - java

I am trying to save an entity using Springs crud repository (repo.save(Entity)) to my sql db - for some reason though when the entity is saved, the entity's variables/payloads are cut off at 32,000 characters in the db. Is there a limit that is causing this? (The column in my SQL db is type LONGTEXT - max of ~4million characters).

Solved it with annotating the column with #Lob, and specifying as columnDefinition = "LONGTEXT"

Related

Does spring.jpa.hibernate.ddl-auto = validate property validates the columns with table?

In my application i am using below property
spring.jpa.properties.hibernate.hbm2ddl.auto= validate
so does it validates schema like if manually i added one new column in my oracle table and not added this column in java entity .
so in this case will it give error on project startup ?
No, it's perfectly alright to have columns in a table that aren't mapped. An error would be a missing column or table that's mapped to an entity. Your database doesn't need to be identical to your mappings.

With DataJpaTest Repository Tests using MySQL, Hibernate doesn't set User to auto-increment in H2 DB

I'm trying to test my Spring Boot repositories using DataJpaTest. I'm using MySQL, so all of my models are using IDENTITY for id generation:
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
But as you can see from the screenshot below, when I run my tests, Hibernate sets "generated by default as identity" for the ids of all of my models except for User and Property:
This makes it so that I cannot create users (or properties) in my tests, since GenerationType.IDENTITY sets always sets the id to null before sending it to the db, which results in the following error:
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
Does anyone know why this is happening? And, of course, more important than the why is what can I do to fix it? :)
Update
To simplify the problem a bit and provide different errors for different things I've tried...
My DataJpaTests are using H2, which I guess is automagically generating the db structure from the code. My real DB is MYSQL, which is generated in a separate project.
Scenario 1:
When I set GenerationType to IDENTITY, the live DB works, but the test DB gives me could not execute statement; SQL [n/a]; constraint [null].
Scenario 2:
When I set GenerationType to AUTO, the test DB works, but the live DB gives me Table 'tablename.hibernate_sequence' doesn't exist.
Scenario 3:
Trying to make MYSQL work with GenerationType AUTO. I have found nothing in quite a lot of searching on how to tell Hibernate to default MYSQL to identity instead of table. I've found rather a lot saying to never use table and that overriding this is impossible without changing the Hibernate source code. This is a bunch of BS that makes me wonder what the Hibernate developers are smoking.
Scenario 4:
Trying to make H2 work with GenerationType IDENTITY. I've had some luck here with the following:
Putting spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=MYSQL in a test.properties file.
Annotating my Test class with #TestPropertySource(locations= "classpath:test.properties") and #AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
This gets rid of the null constraint error, but instead I'm now getting The database returned no natively generated identity value. Presumably, this is because, while the actual SQL code had auto_increment for the id, whatever magic is being used to generate the table for H2 isn't getting it there.
Addendum: The thing that is really frustrating about all of this is that it works totally fine for most of the tables. Out of the more than 20 tables in my DB, all but two of them auto generate and increment their ids just fine using IDENTITY. It makes no sense at all that some of them would work and some of them wouldn't.
The problem was another model with #Table(name = "user") as an annotation. That model also had the Id column defined, but without a generated value. With the conflict, Hibernate chose that one to define the id.
MySql works better with
#GeneratedValue(strategy = GenerationType.AUTO)
its also better because it produces auto-increment primary key in most RDBMS

CLOB not saving on ORACLE (with JPA, eclipselink)

I'm working on a project in which I have a class with this attribute:
#Column(name="XMLDATA", columnDefinition="CLOB NOT NULL") #Lob #Basic(fetch=FetchType.LAZY)
#Getter #Setter private String _xmlData;
In the database, the column type for that attribute is CLOB.
I'm running the application on weblogic 11gR1 (10.3.5), using Oracle database (11gR2), JPA and eclipselink as JPA Provider. The problem I have is that everytime I save a registry on the database, the clob is the only field that is not saved.
But if I don't put the #lob annotation, then it works fine.
In the logs, I can see that first it uses an "insert" sentence to insert all the data except for the clob, and then it uses a "select for update" sentence to insert the clob data, I don't know if this may be part of the problem.
Thank you.
For the proper management of the blobs (JPA + Oracle) you must specify the following properties of eclipselink :
property name="eclipselink.target-database"
value= "org.eclipse.persistence.platform.database.oracle.Oracle11Platform"
It seems that JPA sometimes cannot detect the correct version of Oracle Database platform
I've searched a lot for this answer, until I realized that if you are using EclipseLink, at least 2.5.2 version, you don't have to use "#Column(name="XMLDATA", columnDefinition="CLOB NOT NULL") #Lob #Basic(fetch=FetchType.LAZY)". Just do this:
#Column(name = "XML_DATA")
private String _xmlData;
EclipseLink will know how to handle the CLOB column.

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.

JPA JoinColumn Annotation

In my JPA class, I have this Annotation and the syntax I am not able to understand
#JoinColumns({
#JoinColumn(name="RES_ID", referencedColumnName="ACCT_ID"),
#JoinColumn(name="DELETED", referencedColumnName="'N'")
})
protected Account account;
The first line is ok: The current class has column in db (RES_ID) which joins with Account which has a column ACCT_ID
But the second line says :
#JoinColumn(name="DELETED", referencedColumnName="'N'")
Now both these tables have a column called DELETED. Is that a shorthand way of saying that join the two tables when both these tables have DELETED = 'N'?
Because the documentation says that referencedColumn should contain a columnName. here it is containing a value = N
Let me guess, you're using OpenJPA? This is surely not a specified JPA feature, but OpenJPA has such a feature called constant joins in its Non-Standard Joins.

Categories