Store a big String in database? - java

What is the best way to store a a big String in SQL server Database.
I'm using varchar(8000) but I get this exception while persisting my Object using Hibernate :
>Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: String or binary data would be truncated.

Which version of SQL Server are you using? If the version is above 2005 Use VARCHAR(MAX). If the version is before 2005 you can use TEXT

When you want to store a very big string on SQL Server you can use:
varchar(max)

Well as I can see the tag of hibernate you need the tag of #Lob for storing super large objects.
Specifies that a persistent property or field should be persisted as a
large object to a database-supported large objec
Example:
#Lob #Basic(fetch=LAZY)
#Column(name="REPORT")
protected String report;
From Hibernate Docs.

Related

JPA UUID mapping for Oracle ORA-01465: invalid hex number

While this mapping works in MySQL on saving objects:
#Id
private String id;
on Oracle it throws: ORA-01465: invalid hex number when I am saving my object.
This is how I create id: UUID.randomUUID().toString()
My app must support both MySQL 5 and Oracle 12. So I can add only some mysql / oracle specific adapters / extensions that could be easily turned off while switching from one db to another. I cannot change JPA entities code if that would mean binding them to specific database. It must work on both databases.
What could I do so that it wouldn't break the application while switching from one MySQL to Oracle ?
Just remove '-' from UUID.randomUUID().toString()
For example,
UUID.randomUUID().toString().replaceAll("-","")

How to create a Text type field in db via Hibernate+Java

I do have a Java Web Applicaiton (struts2, hibernate, beans) + PostreSQL as DB. The task is to save the base64 encoded text in the db for some specific table. That base64 is generated from pdf file, which is then ciphered with a specific algorithm. The pdf files <1mb, mostly <300kb.
I did a search and it's suggested to save the base64 as a Text field in the DB. It's not problem to create it within the PostgreSQL itself, but I have to create it via a Model class + hibernate.
What I did:
Imported import org.apache.struts2.components.Text;
Generated getters/setters. Added one row to my *.hbm.xml file.
<property name="base64signed" column="base64signed" />
And I got this error:
Could not determine type for: org.apache.struts2.components.Text
I think you should go with this annotation :
#Lob(type = LobType.CLOB)
I don't think Hibernate supports conversion of org.apache.struts2.components.Text to DB's varchar.
So you store it as LOB or CLOB as mentioned in above
#Lob
private Text base64signed;
Or you can make it easy by declaring your 'base64signed' field as String, it will take less memory in DB
#Column
private String base64signed;

Clob to String using hibernate and Oracle

I am having difficulty while reading from oracle database using hibernate. The column is of clob type and mapped class property is of String type. The database is Oracle 11G. I have tried to update my driver as suggested by some posts, But it was of no use. The problem is that All other columns(which are not clob) are read properly and the column which is clob is returned null besides it has data. Thanks in advance.
The query is :
select id,about_us,other_details,periodicity,active,createts,updatets from Details where id = ?
This above the HQL query where about_us and other_details are clob type in database. The java entity contains it as type String.
Rahul
I tried many solutions as suggested in different posts, It includes:
1) Updating odbc jar.
2) Using #lob on the porperty in hibernate mapping/entity.
Both of the above solutions did not work for me, Rather I used the hibernate function str(clob_property) to read it, It worked for me and I could get the property read.
Regards
Rahul
Have you tried something like this?
#Lob #Column(name = "long_text")
private String longText;

Unlimited String in JDO # DataNucleus? (H2 DB)

I need to store a pretty much unlimited String (document) and I won't be able to determine it's length. I know there is CLOB, but I couldn't find it's implementation in JDO, just JPA.
I tried that:
#Column(jdbcType="CLOB")
private String contents = "";
But when I run schema validation tool I receive:
An exception was thrown during the operation of SchemaTool. Please refer to the log for full details. The following may help : Incompatible data type for column SCENE.CONTENTS : was VARCHAR (datastore), but type expected was CLOB (metadata). Please check that the type in the datastore and the type specified in the MetaData are consistent.
I must be missing something basic...
You couldn't find CLOB in JDO ? JDO allows you to define "jdbc-type", so you have the full range of types. In JPA you have a very limited set

cannot get clob datatype from database into JPA entity

I able to save (spring-hibernate saveorupdate()) field
#Lob
#Column(name = "FILENAME")
private String filename;
into oracle database datatype is clob
but when i try retrieve it, i get error
ERROR -
JDBCExceptionReporter.logExceptions(72)
| ORA-00932: inconsistent datatypes:
expected - got CLOB
below is how i retrive from database
DetachedCriteria crit = DetachedCriteria.forClass(Storagefile.class);
crit.addOrder(bSortOrder ? Order.asc(sortColumnId) : Order.desc(sortColumnId));
List<Storagefile> result = (List<Storagefile>) getHibernateTemplate().findByCriteria(crit, nFirst, nPageSize);
It's not clear from your sample code, but my guess is that you're trying to sort by the CLOB column, and Oracle does not permit that. That error code is Oracle's charming way of telling you this.
Are you sure you need to use a CLOB to store a filnename? Oracle can store up to 4000 characters in a VARCHAR2 column, surely that's enough for a filename? If you want to sort by the filename, then that's what you'll need to do.
Have you waded through this:
https://www.hibernate.org/56.html
There seems to be an issue with the Oracle 9i driver and LOBs (not sure what your setup is).

Categories