Re-create table with hibernate - java

I used Hibernate to create an annotated class, and it worked fine for the first time I ran the application.
The problem is that I did a DROP TABLE using psql and now I want hibernate to re-create that table automatically again, based on my annotated class.
If I run the application again, the table is not created and I get an Exception saying that such table doesn't exist (when I try to access it).
What should I do to re-create that table, just as I was creating it for the first time?

you can try to set property
hibernate.hbm2ddl.auto = update
or generate ddl manually with
java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaExport options mapping_files
with this tool you can export ddl to a file and execute script manually or do it automatically (mapping_files is optional)

Related

AS400 DB2 SQL7008 : Table name not valid for operations when inserting a record via Spring Boot

We have schemas / libraries created directly by OS/400 commands in DB2. Hence journaling will not be enabled by default for any physical file (table) If we would create newly. We are using DB Migration tool like liquibase for all DB changes like table / view creation in spring boot. while trying to insert or update, I am getting error "java.sql.SQLException: [SQL7008] X in TABLE_NAME not valid for operation". This error is due to the journaling not done on the newly created table via liquibase. Now, I am trying to find the below possibilities If available
Is there any possibility of creating table (SQL) under the DB2 library 9created in OS/400) so that the journaling is not required while inserting or updating ?
Is there any possibility of creating a journal on a table via Java/Spring Boot?
or any suggestions rather than journaling the table everytime in DB2 side ?
Please give your comments
When commitment control (transaction isolation) is used, journaling of the tables is required.
You have two options:
turn off commitment control
Turn on journaling for the tables
For option 1, you can include
transaction isolation=none;
in the connection string, see this question for more detail
For option 2, if you use the SQL CREATE SCHEMA and CREATE TABLE commands, to create the library and files, then the tables will be automatically journaled.
You can also use the Start Journal Library (STRJRNLIB) command after creating a library via the Create Library (CRTLIB) command. Thereafter, when you create a table or physical file in the library it will be journaled automatically.
I had this error and fixed it using iAccess client tool.
You can add DB2 journal using IBM i Access client tool.
Steps
Open the schema ,
Right click on Tables , then click include on the right click menu.
MYTABLE (the table I want to add journal) will be appeared on the list, then right click on the table name then and go to journaling, now add the values for Journal and library

Java - Cannot modify Embedded Database table using variable

I'm using Java Netbean with Embedded JavaDB at directory D:. First, when I build this project i can CRUD without problem, but after moving to embedded Database, now i cant use delete or update SQL(using local String variable),
With this SQL it always show :"java Exception: A lock could not be obtained within the time"
"delete from table where id='"+txtID.getText()+"'"
I can only use this
"delete from table where id='0001'"
Both of these codes have same output. Tried to put .toString() but still won't work

What happens if I change name of a field in a grails domain?

I had a grails domain with a field named created Though now I've changed it to dateCreated. However, my database table still has the column named created so anytime I try to save a record grails complains saying Field 'created' doesn't have a default value even though I no longer have this field in my domain.
How does one get around this issue? Do I have to open my db and delete this column? In rails this is handled through migrations, what is the equivalent in grails?
If you just rename the domain field, you can specify column name in the mapping block, and not change it when you rename the related field. So, no DB changes will be needed at all.
class MyDomain {
Date dateCreated
static mapping = {
dateCreated column: 'created'
}
}
You must specified dbCreate = "update" in your DataSource.groovy.
If you are in development stage, you can change it to dbCreate = "create" to enable automatic schema refresh. Otherwise, in a production environment, you have to keep that configuration and alter the table manually.
You may refer to Grails DataSource doc, which also proposed some migration tools:
You can also remove the dbCreate setting completely, which is
recommended once your schema is relatively stable and definitely when
your application and database are deployed in production. Database
changes are then managed through proper migrations, either with SQL
scripts or a migration tool like Liquibase (the Database Migration
plugin uses Liquibase and is tightly integrated with Grails and GORM).

How do I mimic Hibernate hbm2ddl "create" behaviour in Liquibase?

I've worked with liquibase 1.9.5 for a while now and got it to replace hibernate hbm2ddl strategy of creating tables and loading fixtures in it. Since it's a maven project and since I use hsqldb (using file create=true), I simply create the db in the target folder so that I have a fresh database anytime I test the application. Works fine till that I realize:
1 I will need the database to be recreated when doing integration test using mysql database now
2 I will definitely need the same solution for a non maven project.
So basically how do I drop and create the database when using liquibase as opposed to hbm2ddl?
The easiest way is to add a separate database call before liquibase update that runs the sql
DROP DATABASE X;
CREATE DATABASE X
Liquibase does have a dropAll command which can be used to drop everything in a schema, but it is slower than drop/create database on mysql and may miss some database objects.

Autocreate Spring Entity "authorities" during testing

When trying unit tests with Spring Security & Hibernate, none of the security entities "user" or "authorities" are being autocreated. What I have done so far is to write an "user" bo that triggers generation of the appropiate table. However, I am stuck with the authorities:
(as advised by http://java.dzone.com/articles/getting-started-spring for postgresql)
CREATE TABLE authorities
(
username character varying(50) NOT NULL,
authority character varying(50) NOT NULL,
CONSTRAINT fk_authorities_users FOREIGN KEY (username)
REFERENCES users (username) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);
Question: With Hibernate/JPA2, what is the appropiate syntax in order to create a BO representing this query?
Question: Actually, I do not want to create the entry using my own BO. Any better way to make Spring Security or Hibernate create all required tables during test run?
Thanks
Set the hibernate property hibernate.hbm2ddl.auto to update, for example. This should let hibernate automatically create (and update) the tables in needs.
<property name="hibernate.hbm2ddl.auto" value="update" />
Actually, I do not want to create the entry using my own BO. Any better way to make Spring Security or Hibernate create all required tables during test run?
If you don't plan to use Hibernate to interact with these tables, it makes indeed little sense to have Entities for them.
My suggestion would thus be to place the Spring Security tables creation script in an import.sql file and to put this file on the root of the class path and Hibernate will automatically execute it after schema export. See Spring/Hibernate testing: Inserting test data after DDL creation for details (just put your DDL statements on a single line).
Thanks, Pascal, this is just what I have been looking for, however, it does not work. I use maven and put import.sql into the resources dir root (content: CREATE TABLE justatest (aaa character varying(50) NOT NULL );). I also set . Running mvn test copies import.sql to target dir... but nothing happens. logback[debug] does not mention import.sql at all. Any idea where I am going wrong? (Hibernate V 3.5.1-Final)
I'm using this feature with Maven and I cannot reproduce your problem. I have hbm2ddl.auto set to create, my import.sql file is in src/test/resources and it gets executed as expected at the end of the schema export when running tests. Here is the log entry I get (using logback):
20:44:37.949 [main] INFO o.h.tool.hbm2ddl.SchemaExport - Executing import script: /import.sql

Categories