Hibernate: Value too long for column - java

For a project I am working on, we use Spring Boot JPA with H2 database to store certain objects and information. However, after testing the application a bit, I got the following error:
2022-04-03 17:41:19 jdbc[3]: exception
org.h2.jdbc.JdbcSQLDataException: Value too long for column "LIST_OF_PLAYERS VARBINARY(255)": "X'aced0005737200136a6176612e7574696c2e41727261794c6973747881d21d99c7619d03000149000473697a6578700000000a77040000000a7372000e6a61... (262)"; SQL statement:
update lobby set host_id=?, is_public=?, is_started=?, list_of_players=?, token=? where id=? [22001-200]
So I figured that I had to change the maximum allowed number of characters and did the following:
#Column(name = "listOfPlayers", length = 10000)
public ArrayList<Long> playerIds;
It appears that the length attribute only works for VARCHAR and not for VARBINARY. Is it possible to set the maximum for VARBINARY too? If so how?

Related

Unique constraint violated - debug

We have an application which is working for years and we are using same oracle database too. But migrated our database from one host to another host.
DB: ORACLE
Now all of the sudden we are getting following exception,
“org.springframework.dao.DataIntegrityViolationException: ORA-00001: unique constraint (YYY.XXX_LOG_PK) violated;
SQL [n/a]; constraint [YYY.XXX_LOG_PK]; nested exception is org.hibernate.exception.ConstraintViolationException: ORA-00001: unique constraint (YYY.XXX_LOG_PK) violated”
code:
#SequenceGenerator(name = "TT_SEQUENCE_GENERATOR", sequenceName = "YYY.XXX_LOG_SEQ")
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TT_SEQUENCE_GENERATOR")
#Column(name = "ID")
public Long getId() {
return this.id;
}
Sequence in DB:
CREATED 07-NOV-17
LAST_DDL_TIME 07-NOV-17
SEQUENCE_OWNER TT
SEQUENCE_NAME YYY.XXX_LOG_SEQ
MIN_VALUE 1
MAX_VALUE 999999999999999999999999999
INCREMENT_BY 1
CYCLE_FLAG N
ORDER_FLAG N
CACHE_SIZE 0
LAST_NUMBER 75305
Problem:
When we are trying to insert some record through JPA code we are getting the above exception but when I try to insert some record into the DB using sequence.nextval, it is not giving any exception.
Is there anyway I can debug to find out what would be the exception ? I also tried show_sql - I couldn't able to find the solution with this too, as this doesn't print the next sequence number in the console
Please point me in right direction, if you know the solution.
The most common scenario in which a self-augmented sequence encounters a unique constraint conflict is when the data is migrated, causing the maximun value of the data to exceed the sequence value.
Firstly query the sequanence current values:
SELECT seqname.CURRVAL FROM dual
And then modify the sequence value to make sure the sequence's nextval exceeds the current maximum value of the data.
ALTER SEQUENCE seqname INCREMENT BY XXXXXX;
SELECT seqname.NEXTVAL FROM dual;
I found the solution for the problem which we faced.
The problem is from the database end. We found that by reviewing the migration documents and by checking the files.
#Edwin: Thanks for your help, your query also helped me in finding where is the problem residing.
While doing migration the sequences haven't copied from old server to new server. When we copied to new server, everything worked fine.
Thanks everyone.

Hibernate not returning correct value for auto increment column with Oracle 12c

I'm using Hibernate with Oracle 12c.
After executing save operation hibernate not returning correct value of primary key(auto increment) that has sequence in DB.
In the below code UserId column(Auto increment) has a sequence in DB,
After save operation in DB value is 81 but the returning value in hibernate is 3441.(Values are just example)
My code :
`
User user = new User ();
User.setUserName(userName);
User.setRoleId(roleId);
getHibernateTemplate().save(User);
int userId = User.getUserId();
`
Note : Same code working fine with other DBs.
Oracle does not handle the strategy = GenerationType.AUTO like some other dbs. You will need to replace that annotation with something like these two
#GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
#SequenceGenerator(name="my_entity_seq_gen", sequenceName="MY_ENTITY_SEQ")
Explained here as well.

Hibernate - strategy="increment" in table ,Multiple tomcat gives duplicate primary key

Single Database MySql
Multiple Tomcat on different location which shared same code
Hibernate 5.2.2
On Table level
#GenericGenerator(name="employee" , strategy="increment")
#GeneratedValue(generator="employee")
When another server enters value gives duplicate primary key error
In future we are also supporting Sql Server, Oracle, HSQL
Use the following
#GeneratedValue(strategy = GenerationType.IDENTITY)
Increment
generates identifiers of type long, short or int that are
unique only when no other process is inserting data into the same
table. Do not use in a cluster.
Identity
supports identity columns in DB2, MySQL, MS SQL Server,
Sybase and HypersonicSQL. The returned identifier is of type long,
short or int.
Sequence uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or
a generator in Interbase. The returned identifier is of type long,
short or int
Ref : http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html#mapping-declaration-id

MySQL LONGTEXT with h2 embedded database

I am trying to make my application run on MySQL (for production) and H2 (for dev/testing). My (Flyway) script to create the tables is almost identical now, except for a column that needs to be declared 'LONGTEXT' for MySQL. If I also use this for H2 (which is running in MySQL compatibility mode), I get:
Wrong column type in public.public.customer_license for column license.
Found: clob, expected: varchar(65535)
The Java code of my entity:
#Column(name = "license", length = 65535)
private String m_license;
If I change the column declaration to VARCHAR(65535), then it works for H2, but not for MySQL:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Column length too big for column 'license'
(max = 21845); use BLOB or TEXT instead
How can I make it work for both?
I had the same problem. I solved it by using the #Lob Annotation. This validates ok with LONGTEXT in a mysql table. When using an H2 in-memory, a CLOB field is created.
import javax.persistence.Lob;
...
#Lob
private String lotsOfText;
That is one of the reasons there is orm.xml, so you can have one mapping for one datastore, and one for a different datastore, and hence no need to recompile the code between runs

mysql.jdbc exception outside valid range for the datatype INTEGER

I am running into an error when I am trying to access the contents of database (BIGINT) from a Java file. I am not able to figure out the fix for this exception.
I am getting following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLDataException:
'5.62949953421312E15' in column '1' is outside valid range for the datatype INTEGER.
Java code:
long pID=0;
SQLStatement = connection.createStatement();
String Query101 = "select projectID from Project";
ResultSet rs101 = SQLStatement.executeQuery(Query101);
while (rs101.next()) {
pID = rs101.getLong("ProjectId");
}
SQL code:
CREATE TABLE Project (projectID bigint(20) Primary key)
The value for projectID in the database table I am using is 5629499534213120.
your pID is long type, which is 4 bytes, max value is 2^31-1 = 2,147,483,647.
So you cannot assign 5.62949953421312E15 to it at pID = rs101.getLong("ProjectId");
After looking this page over and checking out if there is any known MySQL Connector/J issue. I believe that your ProjectID column is actually an INT (synonym of INTEGER) as the exception suggests. I would look over your Project table and ensure that the column is really a BIGINT.
BIGINT is MySQL's 64-bit signed integer, just like long is Java's 64-bit signed integer. Meaning they share the same range of -263 to 263 - 1.
You are using the correct approach. But I think your column's datatype is not what you think it is.
If you encapsulate this table, you can see whether the transfer type is long when you execute the set operation
like this enter image description here
I was here that did not turn the type to cause this problem

Categories

Resources