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

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

Related

Hibernate: Value too long for column

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?

Java Long Datatype converts to bigint type in oracle

I'm doing a sample program to learn hibernate. One of the variable defined in an Entity is of type Java Long but when I startup application and when Hibernate tries to auto create table in Oracle database it generate create table script with type as bigint.
"private Long pin" converts to "pin bigint", since oracle doesn't support bigint datatype the scripts fails. I have added dialect=org.hibernate.dialect.OracleDialect to hibernate.properties file to generate SQL specific to Oracle. Is there anything else to be done?

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.

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

How can i refresh my primary key column?

Till recent time i was using hibernate #Entity annotation to map to database tables.All the primary keys are annotated with #GeneratedValue(strategy = GenerationType.IDENTITY)
I got a scenario where i need to create new schema + migrate data from old schema into new schema.(with few column changes like drop, length and type)
After successful migration of data to new schema tables when i try to insert data using Application its throwing an exception
[ERROR] util.JDBCExceptionReporter DB2 SQL Error: SQLCODE=-803, SQLSTATE=23505, SQLERRMC=1; _NewSchema_._TableName_ , DRIVER=3.51.90
I believe that application is trying to insert rows again with Primary key value starting from 1 because same application is working fine with empty tables.
I want data rows to be inserted with its primary key value as highest value of existing rows primary key .
Any help will be thank full :)
Yes you can do that by altering the table. Alter the table and set starting index for identity column in DB2.
Suppose maximum rows for TBALE_A is 50 and name of identity column is TABLE_ID
ALTER TABLE TBALE_A ALTER COLUMN TABLE_ID
RESTART WITH 51
Your guess is correct, here is my solution, execute the following SQL to give the ID column a specified start position, then your application will work fine.
alter table TABLE_NAME alter column ID set GENERATED BY DEFAULT RESTART WITH 10000;
Hope to help you :)
In case of generation type , IDENTITY, you should look for identity column to be auto incemental.
#GeneratedValue(strategy = GenerationType.IDENTITY) required primary key column to be auto incremental.

Categories

Resources