I want to create an Entity class with database in Netbeans.
When I select a Data source jdbc/Ionbank (custom Jdbc connection Using JDBC-ODBC bridge with Ms SQL 2005 as database).
I see all the tables from that database.
All tables show no primary key, but they have primary keys in them.
Things I have tried :-
Created new 4-5 data source.
Created tables using query, and not the New table option.
Tried changing Odbc connection.
Tried using different drivers for the Jdbc-Odbc bridge like Sql4jdbc.jar, Jdts.jar.
I had same issue, but i solved it using the following: "New Entity Classes from Database" cannot process some tables, saying "no primary key"
A quote from that link helped me:
The problem will happen if you have foreign keys where upper case and lower case table names don't match the referenced table's definition.
For instance:
create table OkTable (
id int not null auto_increment
, primary key (id)
);
create table MisunderstoodTable(
id int not null auto_increment
oktable int not null
, primary key (id)
, foreign key ok (oktable) references oktable (id)
);
The MisunderstoodTable has a foreign key where the target table name doesn't match the lower/uppercase name of the referenced table.
To avoid this problem, just make sure you type your foreign key definitions while matching upper/lower casing for the targeted table.
Related
I would like to seek your insights regarding the error I'm encountering with my postgresql commands.
Basically, what I want to achieve is to create a "booking" entity with one to one relationship to another table called "booking details". But flyway won't migrate my schema with the following error:
Caused by: org.flywaydb.core.internal.sqlscript.FlywaySqlScriptException:
Migration V0__Initial.sql failed
--------------------------------
SQL State : 42S02
Error Code : 42102
Message : Table "BOOKING_DETAILS" not found; SQL statement:
ALTER TABLE booking ADD CONSTRAINT FK_BOOKING_ON_BOOKING_DETAILS FOREIGN KEY (booking_details_id) REFERENCES booking_details (booking_entity_id) [42102-214]
Line : 16
Statement : ALTER TABLE booking ADD CONSTRAINT FK_BOOKING_ON_BOOKING_DETAILS FOREIGN KEY (booking_details_id) REFERENCES booking_details (booking_entity_id)
Here is my postgresql commands:
DROP SEQUENCE IF EXISTS booking_transaction_sequence;
DROP TABLE IF EXISTS booking;
CREATE SEQUENCE IF NOT EXISTS booking_transaction_sequence START WITH 1000 INCREMENT BY 100;
CREATE TABLE booking (
id BIGINT NOT NULL,
booking_number VARCHAR(255),
booking_status VARCHAR(255),
processed_by VARCHAR(255),
created_at TIMESTAMP WITHOUT TIME ZONE,
booking_details_id BIGINT,
CONSTRAINT pk_booking PRIMARY KEY (id)
);
ALTER TABLE booking ADD CONSTRAINT FK_BOOKING_ON_BOOKING_DETAILS FOREIGN KEY (booking_details_id) REFERENCES booking_details (booking_entity_id);
DROP SEQUENCE IF EXISTS booking_details_transaction_sequence;
DROP TABLE IF EXISTS booking_details;
CREATE SEQUENCE IF NOT EXISTS booking_details_transaction_sequence START WITH 1000 INCREMENT BY 100;
CREATE TABLE booking_details (
booking_entity_id BIGINT NOT NULL,
sender_name VARCHAR(255),
item_details VARCHAR(255),
pickup_address VARCHAR(255),
rider_name VARCHAR(255),
delivery_address VARCHAR(255),
cancellation_reason VARCHAR(255),
CONSTRAINT pk_booking_details PRIMARY KEY (booking_entity_id)
);
ALTER TABLE booking_details ADD CONSTRAINT FK_BOOKING_DETAILS_ON_BOOKINGENTITY FOREIGN KEY (booking_entity_id) REFERENCES booking (id);
I would highly appreciate any inputs regarding this. Thank you.
I tried using the "extend" method on my BookingDetails entity to BookingEntity. This run my java springboot application but for some reason I can't fetch data with internal error 500 in postman. So I change my sql commands with the one-to-one relationship mapping but I got the above errors.
Just out of interest - how you're planning to insert new entries given the schema? In order to create booking you'd need to have booking_details created and vice versa.
The error message indicates that the table "BOOKING_DETAILS" is not found when you're trying to create a foreign key constraint in the "booking" table.
You need to make sure that the "booking_details" table is created before creating the foreign key constraint in the "booking" table. You can do this by reordering your migration script to create the "booking_details" table before creating the "booking" table with the foreign key constraint.
Additionally, check that the table name and column name used in the foreign key constraint statement are correct and match the names used in the "booking_details" table.
Solution: put this command on the last part of the sql so that the two tables must be created first before it can be altered.
"ALTER TABLE booking ADD CONSTRAINT FK_BOOKING_ON_BOOKING_DETAILS FOREIGN KEY (booking_details_id) REFERENCES booking_details (booking_entity_id);"
I have a spring-boot application with a MySql database. I have a many-to-many relationship between a pizza table and a topping table so I've made an extra table where I store these relationships. The tables:
CREATE TABLE topping (
id SERIAL PRIMARY KEY,
topping_name VARCHAR(64) NOT NULL,
price INT NOT NULL,
spicy bool NOT NULL
);
CREATE TABLE pizza (
id SERIAL PRIMARY KEY,
pizza_name VARCHAR(64) NOT NULL
);
CREATE TABLE pizza_with_topping (
pizza_id BIGINT UNSIGNED NOT NULL,
topping_id BIGINT UNSIGNED NOT NULL,
CONSTRAINT pizza_with_topping_ibfk_1
FOREIGN KEY(pizza_id)
REFERENCES pizza(id),
CONSTRAINT pizza_with_topping_ibfk_2
FOREIGN KEY(topping_id)
REFERENCES topping(id)
);
In spring-boot I found that I have to extend the CrudRepository interface and I can call the findAll() method from this to get the contents of a table. At the momment I get the contents of all 3 tables like so:
Iterable<Pizza> pizzasInDb = pizzaRepository.findAll();
Iterable<Topping> toppingsInDb = toppingRepository.findAll();
Iterable<PizzaWithTopping> pizzaToppingConnectionTable = pizzaWithToppingRepository.findAll();
After this based on these 3 tables I manually create objects that contain both the pizza's name and it's toppings. Since I have set foreign keys in pizza_with_topping table I was wondering if there is a better way to get this model? Maybe with the call of built-in functions that automatically makes this model object for me based on the foreign keys.
Yes, there is. You can model your domain with many-to-many relationships using JPA. If you are using annotation this cam be achieve using #ManyToMany
There is an example here
I use Spring Boot and Flyway with this initialization script:
CREATE TABLE ADDRESS(
ID bigserial NOT NULL PRIMARY KEY
);
CREATE TABLE ROLE(
ID bigserial NOT NULL PRIMARY KEY
);
CREATE TABLE PERSON(
ID bigserial NOT NULL PRIMARY KEY,
FIRST_NAME VARCHAR(255),
LAST_NAME VARCHAR(255),
ADDRESS bigserial NOT NULL REFERENCES ADDRESS (ID),
ROLE bigserial REFERENCES ROLE (ID) -- notice here is no 'not null'
);
All the relationship between the tables is that:
Each PERSON has 0-1 ROLE. So, each ROLE belongs to 0-n PERSON. Hence, this relationship is nullable.
Each PERSON has 1 ADDRESS. So, each ADDRESS belongs to 1-n PERSON. Hence, this relationship is not-null.
As soon as I start the application (I have also tried to post the query straight to the PostgreSQL database schema), there is somehow generated constraint not-null between the PERSON and ROLE tables.
Using DataGrip, I select SQL Scripts -> Generate DDL to Query Console and get the DDL for the tables (see below, new lines and roles definitions omitted for sake of brevity).
To my surprise, the NOT NULL is there although I haven't defined such constraint. How to get rid of it aside from altering table?
create table if not exists address
(
id bigserial not null
constraint address_pkey primary key
);
create table if not exists role
(
id bigserial not nullconstraint role_pkey primary key
);
create table if not exists person
(
id bigserial not null
constraint person_pkey primary key,
first_name varchar(255),
last_name varchar(255),
address bigserial not null
constraint person_address_fkey references address,
role bigserial not null -- why is 'not null' here?
constraint person_role_fkey references role
);
The version of PostgreSQL I use (through SELECT version()) is:
PostgreSQL 10.13, compiled by Visual C++ build 1800, 64-bit
"8.1.4. Serial Types":
The data types smallserial, serial and bigserial are not true
types, but merely a notational convenience for creating unique
identifier columns (similar to the AUTO_INCREMENT property supported
by some other databases). In the current implementation, specifying:
CREATE TABLE tablename (
colname SERIAL
);
is equivalent to specifying:
CREATE SEQUENCE tablename_colname_seq AS integer;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
Note the NOT NULL.
Don't use bigserial for the foreign key. That doesn't make much sense. Simply use bigint.
CREATE TABLE IF NOT EXISTS person
(...
role bigint REFERENCES role);
Possible solution 1:
Changing Biserial to Bigint does not remove the null constraint set to foreign key column when running flyway in springboot to write into postgres DB (at least for my case)
postgres:11.3-alphine 3.4
flyway: 8.0.5
To be secure, need to add scripts to alter columns to be nullable
ALTER TABLE mytable ALTER COLUMN mycolumn DROP NOT NULL;
Change Postgres column to nullable
Possible solition 2:
When Spring boot set JPA Hibernate ddl configuration to create, create-drop, update, flyway DB migration script will be updated by JPA entities properties. NOT NULL constraints can be added by JPA entities.
Change JPA Hibernate ddl configuration to none or validate will ensure only flyway script is used to create schema.
JPA Hibernate ddl configuration
In my database I have table "Announcement" with fields:
id int,
text varchar,
sent_date date.
Also table "Group" with fields:
id int,
name varchar.
Each announcement submitted to any number of groups(from 1 to all). How to store this relation in database?
I'm using MySQL and Hibernate in java web project.
Create a third table (I call it announcement_group) with two columns:
announcement_id REFERENCES Announcement(id)
group_id REFERENCES Group (id)
Read more about Foreign Keys from the documentation here http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
In your query you will want to use a
SELECT group_id WHERE announcement_id=<your id here> from announcement_group;
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.