I have the following column in one of my hibernate entities:
#Lob
#Basic(fetch=FetchType.LAZY)
#ColumnTransformer(read = "to_clob(xmlContent)", write = "?")
#Column(name="XMLCONTENT", updatable = false, columnDefinition = "XMLType")
private String xmlContent;
However when I have hibernate.hbm2ddl.auto = create on hibernate it fails to create the particular table giving me the following error:
Unknown data type: "XMLTYPE"; SQL statement:
So basically I can't include this table/entity at all in tests with the H2 database. I need to have an XE at least installed.
Is there any workaround/solution to this, possibly another in memory database supports this?
Related
I wrote program that uses different databases like sql server, oracle etc. My problem is that I can't handle GenerationType and insert correct row into table. Using GenerationType.AUTO and hibernate.id.new_generator_mappings := false in sql server, my program is able to insert new row into table, but ID is always null, same problem is when GenerationType is IDENTITY.
I tried to add auto-incrementation only for sql server, but Liquibase yells at me that it's not supported for mssql. When I use Sequences for Oracle as well SQL Server my program is trying to get "next value" from generator but it cannot and do infinite loop. Even if I set default value for ID it won't increment this value.
Thats my code :
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "name")
#SequenceGenerator(name = "name", sequenceName = "SEQ", allocationSize = 1)
private Long id;
I would like to be able to add auto-incrementing indices into table and it should work for SQL Server databases and I don't want to use Table strategy for generation because it needs additional table in db.
Problem solved. I add condition in Liquibase xml file that checks whether db is mssql type and if it's true script drops ID column and adds it with IDENTITY(1,1) option.
The only problem is that now I have to switch aforementioned "hibernate.id.new_generator_mappings" setting.
I try to prepare an integration test with test data. I read insert queries from an external file and execute them as native queries. After the insertions I execute select setval('vlan_id_seq', 2000, true );. Here is the entity ID definition:
#Id
#Column(name = "id", unique = true, nullable = false)
#GeneratedValue(strategy = IDENTITY)
private Integer id;
When I try tor persist a new entry, I got a Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "vlan_pkey"
Detail: Key (id)=(1) already exists. exception. The ID of the sequence is 2000. The column definition is done by the serial macro and is id integer NOT NULL DEFAULT nextval('vlan_id_seq'::regclass).
I executed the native queries in a user transaction, so all test entries are stored in the postgresql data base, but it seems that hibernate not sync the sequence. The entityManager.flush(); also didn't force a sequence synchronisation. It seems that hibernate did not use sequences with #GeneratedValue(strategy = IDENTITY). I use a XA-Datasource and wildfly 13.
I tested now an other initialisation method. I defined a SQL data script (I generated the script with Jailer) in the persitence.xml (javax.persistence.sql-load-script-source) and end the script with select pg_catalog.setval('vlan_id_seq', (SELECT max(id) FROM vlan), true );. I set a breakpoint before the first persist command, check the sequence in the postgresql db, the sequence has the max id value 16. Now persisting works and the entry has the id 17. The scripts are executed before the entity manager is started and hibernate read the the updated sequences while starting. But this solution did not answer my question.
Is there a possibility that hibernate reread the sequences to use the nextval value?
if the strategy is Identity this means hibernate will create a sequence table and fetch the IDs from it, by using native sql you are just inserting your own values without updating that table so you have TWO solutions
Insert using hibernate itself which will be fairly easy, in your
integration test inject your DAOs and let hibernate do the insertion
for you which is recommended so you do not need to rehandle what
hibernate already handled
Update the sequence table whenever you do the insert by increment the
value which I do not recommend.
Database being used is Oracle 11g
Hibernate: select schemaname.col_sqe.nextval from dual
17:06:58.603 [WARN ] -org.hibernate.engine.jdbc.spi.SqlExceptionHelper:SQL Error: 2289, SQLState: 42000
17:06:58.618 [ERROR] -org.hibernate.engine.jdbc.spi.SqlExceptionHelper:ORA-02289: sequence does not exist
The sequence is present in the specified schema in the database and the same query returns proper value when executed via sql developer.
The Auto generation configuration is as follow:
#Id
#SequenceGenerator(name = "COL_GEN", sequenceName = "COL_SQE",schema="SCHEMANAME")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "COL_GEN")
#Column(name = "COL_ID")
Spring JPA configuration is as follow:
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.hibernate.use-new-id-generator-mappings=true
spring.jpa.show-sql=true
When you use Oracle database, you need to create the sequence manually.
Use this code in your database connection:
create sequence YOUR_SEQUENCE_TABLE_NAME start with YOUR_START_VALUE;
Kindly check if the user which you are using has the read/write privilege
check for read/write privileges and also check the logs from database and verify if the sequence got executed
even though had schema explicitly included, prefixed to sequence name fixed issue
#SequenceGenerator(schema = "schemaname", sequenceName = "schemaname.sequencename")
I migrated my DB from mysql to sql server 2008 and now trying to configure my ORM, but getting this exception when trying to insert a new item into table. I have tested it 2 tables with and without identity column and also GeneretionType.AUTO and GeneretionType.IDENTITY in my entity but it doesnt work.
How can I insert ID field automatically with eclipselink to sql server 2008 ?
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Basic(optional = false)
#NotNull
#Column(name = "SEC_USERS")
private Integer idUser;
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException:
'LAST_INSERT_ID' is not a recognized built-in function name.
Error Code: 195
Call: SELECT LAST_INSERT_ID()
Query: ValueReadQuery(name="SEQ_GEN_IDENTITY" sql="SELECT LAST_INSERT_ID()")
I didn't find the reason behind this exception, but I solved my problem using database table for identifier generation as described here eclipse
I am trying to test a batch reading data from a database.
I have an entity such as:
#Entity
#Table(name = "CLIENT")
public class ClientEntity {
#Id
#Column(name = "ID")
private Long id;
#Column(name = "START", nullable = false)
#Temporal(TemporalType.DATE)
private Date start;
}
When unit testing my batch, I insert data into a memory database thanks to a SQL script read with the hibernate.hbm2ddl.import_files option while the hibernate.hbm2ddl.auto option is set on create:
Insert into CLIENT(ID, START) values (1,'2006-02-01')
Insert into CLIENT(ID, START) values (2,'2010-02-01')
I can see in the log that the table is properly created. Yet, when retrieving the CLIENTs further in my code (perhaps through some join), an exception is raised:
ERROR - Column "CLIENTENTI0_.START" not found; SQL statement:
select cliententi0_.ID as ID1_10_, cliententi0_.START
as START2_10_ from CLIENT cliententi0_ [42122-165]
I have to add that when plugged to an exising Oracle database, the code runs perfectly!
What is wrong with my code? How can I get it to work?
Thanks for your help!
Well shame on me.
The error came from the fact that the goal of my batch is reading data from a database A and to write data to a database B.
To test my batch, I set two memory instances. But a mistaken copy and paste made me create twice the same instance... And since there is one CLIENT table in A and one other CLIENT table in B, an error was raised when retrieving the data: one of those tables was unproperly created!
Hope this might help someone else!