i have a question, i duplicated a table with:
Table<?> table = DSL.table(DSL.name("dc2", "process"));
dsl.createTable(table).as(dsl.select().from("dc1.process")).withNoData().execute();
but doesn't copy a primary key and foreignkey, how i do?
and if i want to duplicate schema in jooq?
thanks
Giuseppe
If you're using the code generator to produce your original schema, you can duplicate it relatively easily using:
Runtime schema mapping: https://www.jooq.org/doc/latest/manual/code-generation/codegen-advanced/codegen-config-database/codegen-database-catalog-and-schema-mapping/
DSLContext.ddl() and/or Meta.ddl()
A more convenient syntax will be supported in the future via standard SQL CREATE TABLE (.. LIKE othertable) syntax:
https://github.com/jOOQ/jOOQ/issues/8860
Or a synthetic CREATE SCHEMA .. LIKE otherschema syntax:
https://github.com/jOOQ/jOOQ/issues/9527
Related
For example, I have a database type alias defined as follows:
create type aml_acct from varchar(50) not null
Then in the SQL for creating a table, I would have a column definition like this:
create table ACCOUNTS (
.
acct aml_acct,
.
)
In 3.7.3 the Jooq generated code was this:
public final TableField<AmlAccountsRecord, String> ACCT =
createField("acct", org.jooq.impl.SQLDataType.VARCHAR.length(50).nullable(false), this, "");
In 3.12.3 the Jooq generated code is this:
/**
* #deprecated Unknown data type. Please define an explicit {#link org.jooq.Binding} to specify how this type should be handled. Deprecation can be turned off using {#literal <deprecationOnUnknownTypes/>} in your code generator configuration.
*/
#java.lang.Deprecated
public final TableField<AmlAccountsRecord, Object> ACCT = createField(DSL.name("acct"), org.jooq.impl.SQLDataType.OTHER.nullable(false), this, "");
But I can't figure out how to make a Binding class to make this properly handle the aml_acct database type and generate the code as before. Or is there a way to handle this with a ForcedType?
Any ideas or help would be appreciated...
A bug in jOOQ
This is a bug in the jOOQ 3.12 code generator (probably also present in previous releases). Recent releases of the jOOQ code generator have added support for table valued functions and table valued parameters in SQL Server. For that, new SYS and INFORMATION_SCHEMA queries have been written to fetch meta data for SQL Server's code generation. In this case, SYS.ALL_COLUMNS is joined to SYS.TYPES on the USER_TYPE_ID column rather than the SYSTEM_TYPE_ID column.
This will be fixed in 3.13.0 and 3.12.4: https://github.com/jOOQ/jOOQ/issues/9551.
Workaround
The workaround is to use a <forcedType> configuration to force the type of your columns to the wanted data type:
https://www.jooq.org/doc/latest/manual/code-generation/codegen-advanced/codegen-config-database/codegen-database-forced-types/
For example:
<forcedType>
<name>VARCHAR</name>
<includeTypes>aml_acct</includeTypes>
</forcedType>
If you have many such types, you can also use <sql> in the above configuration to match all the columns that should have this forced type applied. This could look as follows:
<forcedType>
<name>VARCHAR</name>
<sql>
select string_agg(o.name + '\.' + c.name, '|')
from sys.all_objects o
join sys.all_columns c on o.object_id = c.object_id
join sys.types u on c.user_type_id = u.user_type_id
join sys.types t on u.system_type_id = t.system_type_id
where u.is_user_defined = 1
and t.name = 'varchar'
</sql>
</forcedType>
See the above documentation link for details.
I am trying to create a Vertica table with JOOQ 3.5.x:
Connection connection = create();
DSLContext dslContext = DSL.using(connection);
Field<String> myColumn = DSL.field("my_column", SQLDataType.VARCHAR);
Table table = DSL.tableByName("my_schema", "my_table");
dslContext.createTable(table)
.column(myColumn, myColumn.getDataType())
.execute();
This fails on Schema "my_schema" does not exist.
I can solve it with:
dslContext.execute("create schema if not exists my_schema");
But is there a more elegant way to create a schema with JOOQ?
Currently JOOQ covers just a subset of the possible DDL statements that can be executed against a server and schema management is not yet included so you have to drop back to plan old SQL.
If you need to do a lot of DDL work you should start to look at the latest version 3.8 as this has extend the capabilities to include
DEFAULT column values in CREATE TABLE or ALTER TABLE statements
IF EXISTS in DROP statements
IF NOT EXISTS in CREATE statements
ALTER TABLE .. { RENAME | RENAME COLUMN | RENAME CONSTRAINT } statements
Version 3.6 added
ALTER TABLE ADD CONSTRAINT (with UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK)
ALTER TABLE DROP CONSTRAINT
CREATE TEMPORARY TABLE
I just want to use JOOQ to generate SQL without it validating tables, columns, etc., and without it generating classes for said tables, columns, etc.
How can I generate a SQL update, and just specify the name of the schema & table with Strings?
Maybe later I'll setup the table-generated Java code, but it's not necessary right now. If I can't use JOOQ without such generated code, then I'll use some other library for now.
Thanks.
You don't have to use source code generation to use jOOQ's DSL API. See, for instance:
http://www.jooq.org/doc/latest/manual/getting-started/use-cases/jooq-as-a-standalone-sql-builder
http://www.jooq.org/doc/latest/manual/sql-building/plain-sql
http://www.jooq.org/doc/latest/manual/sql-building/names
In your case, given you want to generate a SQL update, how about:
// Assuming this static import
import static org.jooq.impl.DSL.*;
using(configuration)
.update(table("my_table"))
.set(field("id", Integer.class), 1)
.set(field("value", String.class), "A")
.where("x > ?", 3)
.execute();
jOOQ has CREATE TABLE syntax as stated in the documentation:
create.createTable(AUTHOR)
.column(AUTHOR.ID, SQLDataType.INTEGER)
.column(AUTHOR.FIRST_NAME, SQLDataType.VARCHAR.length(50))
.column(AUTHOR_LAST_NAME, SQLDataType.VARCHAR.length(50))
.execute();
I'm wondering how to define which column belongs to the primary key? So is there a way in jOOQ to create a CREATE TABLE statement with PRIMARY KEY information?
I'm specifically interested in a solution for SQLite, which doesn't have syntax to add the primary key afterwards, so I think in worst case I have to go to a DB specific solution?
This feature has been implemented for jOOQ 3.8: #4050.
create.createTable(AUTHOR)
.column(AUTHOR.ID, SQLDataType.INTEGER)
.column(AUTHOR.FIRST_NAME, SQLDataType.VARCHAR.length(50))
.column(AUTHOR_LAST_NAME, SQLDataType.VARCHAR.length(50))
.constraints(
constraint("PK_AUTHOR").primaryKey(AUTHOR.ID)
)
.execute();
Since jOOQ 3.6 (#3338), you can also use the ALTER TABLE statement to add a constraint after creating the table:
create.alterTable(AUTHOR)
.add(constraint("PK_AUTHOR").primaryKey(AUTHOR.ID))
.execute();
have created my tables and relationships in the database PostgreSQL, but when I want to generate Hibernate Mapping Files and POJOs, they are not generated
I applied all the appropriate steps to hibernate.cfg.xml generation and hibernate.reveng.xml
I think it's because the name tables and fields that I have in all uppercase, because I tested with another BD with the names of the tables in lower case and if it works normally, I show the script of my tables.
CREATE TABLE "public"."T_LNEA"(
"ID_LNEA" Integer NOT NULL,
"ID_CTGRIA" Integer NOT NULL,
"DSCRPCION" Character varying(200)
)
WITH (OIDS=FALSE)
;
ALTER TABLE "public"."T_LNEA" ADD CONSTRAINT "PK_ID_LNEA" PRIMARY KEY ("ID_LNEA")
;
CREATE TABLE "public"."T_SUB_LNEA"(
"ID_SUB_LNEA" Integer NOT NULL,
"ID_LNEA" Integer NOT NULL,
"DSCRPCION" Character varying(200)
)
WITH (OIDS=FALSE)
;
-- Add keys for table public.T_SUB_LNEA
ALTER TABLE "public"."T_SUB_LNEA" ADD CONSTRAINT "PK_ID_SUB_LNEA" PRIMARY KEY ("ID_SUB_LNEA")
;
CREATE TABLE "public"."T_CTGRIA"(
"ID_CTGRIA" Integer NOT NULL,
"DSCRPCION" Character varying(200)
)
WITH (OIDS=FALSE)
;
ALTER TABLE "public"."T_CTGRIA" ADD CONSTRAINT "PK_ID_CRITERIA" PRIMARY KEY ("ID_CTGRIA")
;
And an image that is loading the tables using the JBOS Tools.
But still I need support because I can not generate the POJOs.
In the File "hibernate.reveng.xml" has the following
<schema-selection match-catalog="mybd" match-schema="mybd"/>
You have to delete the match-schema="mybd".
By default, when you use postgresql in hibernate, appears the match- schema , which does not happen in mysql , delete it and work , checked in Netbeans8.0
Hi, as said before, if I change the name of the table and its fields in lowercase, if generates POJOs, the question is why is not generated in capital letters .....
Une the reverse engineering tools provided by Hibernate. It depends on your IDE.
For example with NetBeans this is how it is done: https://netbeans.org/kb/docs/web/hibernate-webapp.html
You solve it somehow does not recognize the table name in uppercase, but if you recognize the attributes of the table in uppercase, so rename the tables in lowercase temporarily after he change in the EJB annotations, at least it works somehow.