NetBeans Generated REST Service works for XML requests, but not JSON - rest

I am using NetBeans to help me build a REST web service. My steps taken have been the following:
New Project -> Web Application
New File -> Web Service -> RESTFul WebService from Database
Select the Datasource (which is a MySQL DB)
Everything generates, right click on project -> Test RESTFul
WebServices
When I want to test GET(application/XML), I can get a result returned fine, I can also get an XML response from a Jersey Client I made after the fact. But when I test the JSON functions, I always seem to get errors:
exception
javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper
root cause
org.glassfish.jersey.server.ContainerException:
java.lang.NoClassDefFoundError: Could not initialize class
org.eclipse.persistence.jaxb.BeanValidationHelper
root cause
java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper
My Database schema, which would really be the only thing I've written myself, is the following:
-- ****************** SqlDBM: MySQL ******************;
-- ***************************************************;
DROP TABLE `roles`;
DROP TABLE `orders`;
DROP TABLE `inventory`;
DROP TABLE `users`;
DROP TABLE `warehouses`;
DROP TABLE `inventory`;
DROP TABLE `addresses`;
DROP TABLE `products`;
-- ************************************** `addresses`
CREATE TABLE `addresses`
(
`addressID` INTEGER NOT NULL AUTO_INCREMENT ,
`streetNum` INTEGER NOT NULL ,
`streetName` VARCHAR(45) NOT NULL ,
`unitNum` INTEGER ,
`city` VARCHAR(45) NOT NULL ,
`province` VARCHAR(45) NOT NULL ,
`postalCode` VARCHAR(6) NOT NULL ,
PRIMARY KEY (`addressID`)
);
-- ************************************** `products`
CREATE TABLE `products`
(
`productID` INTEGER NOT NULL AUTO_INCREMENT ,
`productNO` VARCHAR(40) NOT NULL ,
`productName` VARCHAR(80) NOT NULL ,
`productDesc` VARCHAR(160) NOT NULL ,
`cost` REAL NOT NULL ,
`price` REAL NOT NULL ,
PRIMARY KEY (`productID`)
);
-- ************************************** `users`
CREATE TABLE `users`
(
`userID` INTEGER NOT NULL AUTO_INCREMENT ,
`firstName` VARCHAR(20) NOT NULL ,
`lastName` VARCHAR(20) NOT NULL ,
`dateOfBirth` DATE NOT NULL ,
`email` VARCHAR(45) NOT NULL ,
`password` CHAR(64) NOT NULL ,
`addressID` INTEGER NOT NULL ,
PRIMARY KEY (`userID`),
KEY `fkIdx_87` (`addressID`),
CONSTRAINT `FK_87` FOREIGN KEY `fkIdx_87` (`addressID`) REFERENCES `addresses` (`addressID`)
);
-- ************************************** `warehouses`
CREATE TABLE `warehouses`
(
`warehouseID` INTEGER NOT NULL AUTO_INCREMENT ,
`addressID` INTEGER NOT NULL ,
PRIMARY KEY (`warehouseID`),
KEY `fkIdx_58` (`addressID`),
CONSTRAINT `FK_58` FOREIGN KEY `fkIdx_58` (`addressID`) REFERENCES `addresses` (`addressID`)
);
-- ************************************** `inventory`
CREATE TABLE `inventory`
(
`id` INTEGER NOT NULL AUTO_INCREMENT ,
`productID` INTEGER NOT NULL ,
`warehouseID` INTEGER NOT NULL ,
`expiry` DATE NOT NULL ,
`productID_1` INTEGER NOT NULL ,
PRIMARY KEY (`id`),
KEY `fkIdx_31` (`productID_1`),
CONSTRAINT `FK_31` FOREIGN KEY `fkIdx_31` (`productID_1`) REFERENCES `products` (`productID`)
);
-- ************************************** `roles`
CREATE TABLE `roles`
(
`roleID` INTEGER NOT NULL AUTO_INCREMENT ,
`roleName` VARCHAR(20) NOT NULL ,
`userID` INTEGER NOT NULL ,
PRIMARY KEY (`roleID`),
KEY `fkIdx_96` (`userID`),
CONSTRAINT `FK_96` FOREIGN KEY `fkIdx_96` (`userID`) REFERENCES `users` (`userID`)
);
-- ************************************** `orders`
CREATE TABLE `orders`
(
`orderID` INTEGER NOT NULL AUTO_INCREMENT ,
`orderNum` INTEGER NOT NULL ,
`productID` INTEGER NOT NULL ,
`quantity` INTEGER NOT NULL ,
`userID` INTEGER NOT NULL ,
PRIMARY KEY (`orderID`),
KEY `fkIdx_70` (`productID`),
CONSTRAINT `FK_70` FOREIGN KEY `fkIdx_70` (`productID`) REFERENCES `products` (`productID`),
KEY `fkIdx_100` (`userID`),
CONSTRAINT `FK_100` FOREIGN KEY `fkIdx_100` (`userID`) REFERENCES `users` (`userID`)
);
-- ************************************** `inventory`
CREATE TABLE `inventory`
(
`id` INTEGER NOT NULL AUTO_INCREMENT ,
`expiry` DATE ,
`productID` INTEGER NOT NULL ,
`warehouseID` INTEGER NOT NULL ,
PRIMARY KEY (`id`),
KEY `fkIdx_40` (`productID`),
CONSTRAINT `FK_40` FOREIGN KEY `fkIdx_40` (`productID`) REFERENCES `products` (`productID`),
KEY `fkIdx_62` (`warehouseID`),
CONSTRAINT `FK_62` FOREIGN KEY `fkIdx_62` (`warehouseID`) REFERENCES `warehouses` (`warehouseID`)
);

Related

postgreSQL syntax error at or near "foreign"

I get the following error of "syntax error at or near "foreign"" when trying to create kurinys table. Might be a silly mistake but I can't recognize it. Thank you in advance.
create table elba7430.kurinys(
id INTEGER not null check (id > 10000),
pavadinimas VARCHAR (55) not null,
metai YEAR,
meno_rusies_id INTEGER not null check (meno_rusies_id > 100),
autoriaus_id INTEGER not null check (autoriaus_id > 1000000),
kliento_id INTEGER not null check (kliento_id > 1000000),
ilgis_cm DECIMAL (100,2),
plotis_cm DECIMAL (100,2),
kaina DECIMAL (100,2),
primary key (id),
foreign key (meno_rusies_id)
REFERENCES elba7430.meno_rusis on delete cascade on update restrict,
foreign key (autoriaus_id)
REFERENCES elba7430.autorius on delete cascade on update restrict,
foreign key (kliento_id)
REFERENCES elba7430.klientas on delete cascade on update restrict
);
create table elba7430.meno_rusis(
id INTEGER not null check (id > 100),
pavadinimas VARCHAR (100) not null,
primary key(id)
);
create table elba7430.autorius(
id INTEGER not null check (id > 1000000),
vardas VARCHAR (40) not null,
pavarde VARCHAR (55) not null,
gimimo_metai DATE,
primary key(id)
);
create table elba7430.klientas(
id INTEGER not null check (id > 1000000),
vardas VARCHAR (40),
pavarde VARCHAR (55),
primary key(id)
);
Changing the order of your declarations and replacing the year data type by a valid one will solve this issue, here you can replicate this: db<>fiddle

Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'questions_ibfk_1' in the referenced table 'category'

cannot add foreign key constraint to table
create table users
(
user_id int auto_increment primary key not null,
username varchar(50) unique null ,
email varchar(50) unique ,
passwords varchar(50) not null,
login_status boolean not null
);
create table category (
category_id int primary key not null,
category_name varchar(50) not null
);
create table answers (
id_answer int auto_increment primary key not null,
answer boolean not null
);
create table questions (
question_id int primary key not null,
category_name varchar(50) not null,
content varchar(50) not null ,
foreign key (category_name) references category (category_name)
);
You get this error because there's no index on category_name in the category table. Change that CREATE statement as follows:
create table category (
category_id int primary key not null,
category_name varchar(50) not null,
KEY category_name_index (category_name)
);
From the docs (8.0 version, but the statement is true for older versions):
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.
Also, you're using a varchar(50) as your foreign key, which is not usually a great idea for a variety of reasons. You probably want to use a numeric value, such as category_id, instead.

How to connect graphql server to postgres database?

This is myposgres.sql file in my project directory:
CREATE TABLE client (
cli_id serial PRIMARY KEY,
cli_name text NOT NULL,
cli_birthyear smallint NOT NULL
);
CREATE TABLE product (
pro_id serial PRIMARY KEY,
pro_ean text UNIQUE NOT NULL,
pro_name text NOT NULL,
pro_description text NOT NULL,
pro_weight_g smallint NOT NULL
);
CREATE TABLE command (
com_id serial PRIMARY KEY,
com_number text UNIQUE NOT NULL,
cli_id integer NOT NULL REFERENCES client
);
CREATE TABLE command_line (
lin_id serial PRIMARY KEY,
com_id integer NOT NULL REFERENCES command,
pro_id integer NOT NULL REFERENCES product,
lin_quantity smallint NOT NULL
);
Now, how do I use this file to connect the postges database to graphql? And how query postgres database using graphql?

pgAdmin - When trying to make a foreign key "referencing" gives no options

I have three tables: ModelingAgency.clients, ModelingAgency.models, ModelingAgency.Bookings. All three tables have a primary key column called id.
The table bookings has two columns that reference clients and models. In pgAdmin when I try to create a foreign key in bookings to either clients or models I get the following screens:
What am I overlooking here? I am new to PostgreSQL (This is my first test project with PostgreSQL -- I've always used MySQL and occasionally SQL Server) so it's probably something obvious (I just don't see it).
EDIT: Here is the DDL, as requested:
-- Table: "ModelingAgency.bookings"
-- DROP TABLE "ModelingAgency.bookings";
CREATE TABLE "ModelingAgency.bookings"
(
id integer NOT NULL DEFAULT nextval('"ModelingAgency.Bookings_id_seq"'::regclass),
"clientId" integer NOT NULL,
"modelId" integer NOT NULL,
"time" timestamp with time zone NOT NULL DEFAULT now(),
"location" character varying(100) NOT NULL DEFAULT 'No Location Selected'::character varying,
CONSTRAINT "bookingId" PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE "ModelingAgency.bookings" OWNER TO "MyBatisTutorial";
-- Table: "ModelingAgency.clients"
-- DROP TABLE "ModelingAgency.clients";
CREATE TABLE "ModelingAgency.clients"
(
id integer NOT NULL DEFAULT nextval('"ModelAgency.clients_id_seq"'::regclass),
"name" character varying(45) NOT NULL,
CONSTRAINT "clientId" PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE "ModelingAgency.clients" OWNER TO "MyBatisTutorial";
-- Table: "ModelingAgency.models"
-- DROP TABLE "ModelingAgency.models";
CREATE TABLE "ModelingAgency.models"
(
id serial NOT NULL,
"name" character varying(45) NOT NULL,
CONSTRAINT "modelId" PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE "ModelingAgency.models" OWNER TO "MyBatisTutorial";
Looking into your posted DDL code I see that your table's names are written in wrong way (that causes your issue with pgAdmin):
"ModelingAgency.bookings"
It should be in format "schema"."tableName":
"ModelingAgency"."bookings"
After that Object browser looks like this (probably you need to create schema first using easily pgAdmin or with CREATE SCHEMA SQL statement):
Here is working DDL code (I omitted some things like OIDS and OWNER TO, but that doesn't matter to your case, BTW OIDS are false on default):
DROP TABLE IF EXISTS "ModelingAgency"."bookings";
CREATE TABLE "ModelingAgency"."bookings"
(
id serial,
"clientId" integer NOT NULL,
"modelId" integer NOT NULL,
"time" timestamp with time zone NOT NULL DEFAULT now(),
"location" character varying(100) NOT NULL
DEFAULT 'No Location Selected'::character varying,
CONSTRAINT "bookingId" PRIMARY KEY (id)
);
DROP TABLE IF EXISTS "ModelingAgency"."clients";
CREATE TABLE "ModelingAgency"."clients"
(
id serial,
"name" character varying(45) NOT NULL,
CONSTRAINT "clientId" PRIMARY KEY (id)
);
DROP TABLE IF EXISTS "ModelingAgency"."models";
CREATE TABLE "ModelingAgency"."models"
(
id serial NOT NULL,
"name" character varying(45) NOT NULL,
CONSTRAINT "modelId" PRIMARY KEY (id)
)

Junction tables in T-SQL

In my database application, when I try to attach a page to a site, I get the following error:
System.InvalidOperationException: Can't perform Create, Update or Delete operations on 'Table(Junc_Page_Site)' because it has no primary key.
I intend a page to be able to be added to multiple sites - by using a junction table.
Below is the schema for the three tables in question (T-SQL). Any advice will be appreciated!
CREATE TABLE Site (
Id INTEGER NOT NULL IDENTITY ,
FkSiteId INTEGER ,
Name NVARCHAR(64) NOT NULL ,
Domain NVARCHAR(128) ,
Theme NVARCHAR(32) ,
ThemeVariation NVARCHAR(32) ,
PRIMARY KEY(Id) ,
FOREIGN KEY(FkSiteId)
REFERENCES Site(Id));
GO
CREATE TABLE Page (
Id INTEGER NOT NULL IDENTITY ,
Title NVARCHAR(64) NOT NULL ,
Description NVARCHAR(256) NOT NULL ,
Keywords NVARCHAR(1024) NOT NULL ,
ScriptName NVARCHAR(64) ,
PRIMARY KEY(Id));
GO
CREATE TABLE Junc_Page_Site (
FkPageId INTEGER NOT NULL ,
FkSiteId INTEGER NOT NULL ,
FOREIGN KEY(FkSiteId)
REFERENCES Site(Id),
FOREIGN KEY(FkPageId)
REFERENCES Page(Id));
GO
You're almost there. You just need to create what's called a "composite primary key", which is to say that you need to tell the database that the conbination of FkPageId and FkSiteId will be unique:
CREATE TABLE Junc_Page_Site (
FkPageId INTEGER NOT NULL ,
FkSiteId INTEGER NOT NULL ,
FOREIGN KEY(FkSiteId)
REFERENCES Site(Id),
FOREIGN KEY(FkPageId)
REFERENCES Page(Id),
PRIMARY KEY (FkPageId, FkSiteId));