JPA serial numbers using a customer ID generation - java

I get this error when I try to generate serial numbers for ID using a random generator instead of the built-in strategies:
run:
[EL Info]: 2015-03-27 18:22:05.047--ServerSession(1185812646)--EclipseLink, version: Eclipse Persistence Services - 2.5.0.v20130507-3faac2b
[EL Info]: connection: 2015-03-27 18:22:09.295--ServerSession(1185812646)--file:/C:/Users/Sobhie/Desktop/people/build/classes/_peoplePU login successful
[EL Warning]: 2015-03-27 18:22:09.498--ServerSession(1185812646)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: BLOB/TEXT column 'R' used in key specification without a key length
Error Code: 1170
Call: CREATE TABLE PERSON (R LONGBLOB NOT NULL, FNAME VARCHAR(255), ID BIGINT, LNAME VARCHAR(255), X INTEGER, PRIMARY KEY (R))
Query: DataModifyQuery(sql="CREATE TABLE PERSON (R LONGBLOB NOT NULL, FNAME VARCHAR(255), ID BIGINT, LNAME VARCHAR(255), X INTEGER, PRIMARY KEY (R))")
BUILD SUCCESSFUL (total time: 13 seconds)

You current custom primary key strategy result in primary keys mapped as blobs (so probably a serialized object)).
Out of the box, BLOB or TEXT cannot be used as PRIMARY KEY in mysql as
a) mysql has limit on how many characeters can form part of the index (so whole TEXT content is out of the question),
b) there is no size constraint for TEXT (for example TEXT(512))
To use TEXT as Primary Key, the index length must be declared explicit (so mysql will look only at the first X characters).
CREATE TABLE PERSON (R TEXT NOT NULL, FNAME VARCHAR(255), ..., KEY ix_length_r (R(255)))
If your R values are not unique in the first XX characters it will not work.
But the mysql ix limit lenght, comes down to 255 for unicodecharacters and to 765 for latin-1. So for unicode you are basically as good as using VARCHAR(255) as your primary key.
You mentioned random number generator for your primary keys but they are mapped as blobs. I don't believe having keys longer than 255 chars makes any sense (it is a huge space about 2E462, so more than atoms in the universe!!!), so you should limit yourself to having them as simple varchar.
But most likely you wanted to use simple Long, but you messed up with your mappings. Impossible to say without the code though.

Related

integer on 4 positions on apache derby

I want the ID for the client to be on 4 positions like "0007" on apache derby but the following request doesn't work:
create table client(id int (5) not null AUTO_INCREMENT primary key, fname varchar(20) not null, lname varchar(20) not null,phnum int(10) not null, email varchar(60) not null ) ;
it throws this exception:
[Exception, Error code 30 000, SQLState 42X01] Erreur de syntaxe : Encountered "(" at line 1, column 28.
how can I make it happen ?
The Derby int has no width. It is soley an integer.
You may use formatting, to use the id as a 4 digit wide string.
Probably you will use your Derby data later in Java thrua a ORM. It is not recomended to use business data as an object id. Let the id an auto-incrementet value to identify your object. You may add another field (like code) to have the client code stored (char(4) with a unique index to avoid duplicates).
(4. Only preview 10.000 clients, that seems a quiet low limit)

Accurate tables for Database normalization

I am thinking on how could I normalize as accurate as possible my tables in my database.
I have to develop a monitoring system where the system should analyse all the outgoing messages(Emails, SMS..) send to customers and their related feedback(status(0,1 or more)).
It means that for 1 outgoing message(With for ex MAILID: 123) I can have multiple different feedbacks(SENT, NOT DELIVERED, DELIVERED, CLICKED, OPENED...) received at different time intervals. For some feedback received I must retrieve the information of what the Email Service provider did in term of "Action". Did they send the email again to the customer?, did they manage to resolve the servers issues if not delivered? etc..
EDIT: I am sorry in fact my MAIL ID columns are all VARCHARS (Java Strings). Long story short, In fact I have to process XML files and unmarshal them to Java Object and persist them into my Mysql dataBase where MAIL ID is in the form of BMM1EP_34022503920_1200180009
To clarify I thought I should have 4 main tables.
Table SentMessages with columns:
MailId VARCHAR primary key(not null)
Form_Name varchar(50)
Language varchar(10)
OutPut_Mode varchar(10)
Table FeedBackMessages with columns:
Id INT AutoIncrement primary key (not null)
MailId VARCHAR
Return_Date DATE
Return_Time DATETIME
FOREIGN KEY (MailId) REFERENCES SentMessages (MailId)
Table Status with columns:
Id Int AutoIncrement primary key (not null)
MailId VARCHAR
Status VARCHAR(50)
FOREIGN KEY (MailId) REFERENCES SentMessages(MailId)
Table Action with columns:
Id Int AutoIncrement primary key(not null)
ActionTaken VARCHAR(100)
MailId VARCHAR
FOREIGN KEY (MailId) REFERNCES Status(Id)
Is my design bad? Help will be highly appreciated as I really struggle it is my first time designing a real database for a concrete project. Thanks to all of you!
It helps if you use a semi-formal natural language to define your business domain. As I understand it:
There are 0..n email messages
Each email message will have 0..n feedback items
Each feedback item has 1 status, and may have additional action information.
If that's accurate, the design would be something like:
Table SentMessages with columns:
MailId varchar primary key(not null)
Form_Name varchar(50)
Language varchar(10)
OutPut_Mode varchar(10)
Table FeedBackMessages with columns:
Id INT AutoIncrement primary key (not null)
MailId varchar
Return_Date DATE
Return_Time DATETIME
Status VARCHAR(50)
ActionTaken VARCHAR(100)
FOREIGN KEY (MailId) REFERENCES SentMessages (MailId)

Java DB How to Insert Values for Foreign Keys into Table Column

I am using java DB database and NetBeans 8.0 for a desktop application
I am also using a PreparedStatement to query the database.
below is the code for creating the tables.
CREATE TABLE ALUMNUS (
ALUMNUA_ID INT NOT NULL PRIMARY KEY
GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
FIRST_NAME VARCHAR (45),
LAST_NAME VARCHAR (45),
OTHER_NAME VARCHAR (100)
);
CREATE TABLE DUES (
ID INT NOT NULL PRIMARY KEY
GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
PAYMENT_YEAR DATE,
AMOUNT DOUBLE,
ALUMNUS_ID INT
);
--FOREIGN KEY
ALTER TABLE APP.DUES
ADD FOREIGN KEY (ALUMNUS_ID) REFERENCES APP.ALUMNUS(ID);
Now I want to insert, delete and update the foreign key values in APP.DUES table. what is the best option; trigger , stored procedure or the preparedstatement?
An example will be good.
If you want to primarily insert into the DUES table, you would use a sub select in SQL. I havent tested it with Java DB, but it basically looks like:
INSERT INTO DUES(PAYMENT_YEAR, AMOUNT,ALUMNUS_ID)
VALUES(2014, 100.0,
(SELECT ALUMNUA_ID from ALUMNUS where ...));
You need to catch the "not found" error case and prepend a INSERT (and need to catch the duplicate case for that as well).
See also: Insert Data Into Tables Linked by Foreign Key

INSERT with DEFAULT id doesn't work in PostgreSQL

I tried running this statement in Postgres:
insert into field (id, name) values (DEFAULT, 'Me')
and I got this error:
ERROR: null value in column "id" violates not-null constraint
I ended up having to manually set the id. The problem with that is when my app inserts a record I get a duplicate key error. I am building a java app using Play framework and ebean ORM. So the entire schema is generated automatically by ebean. In this case, what is the best practice for inserting a record manually into my db?
Edit:
Here is how I'm creating my Field class
#Entity
public class Field {
#id
public Long id;
public String name;
}
Edit:
I checked the field_seq sequence and it looks like this:
CREATE SEQUENCE public.field_seq INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 CACHE 1;
Edit:
Here is the generated SQL in pgAdmin III:
CREATE TABLE field
(
id bigint NOT NULL,
created timestamp without time zone,
modified timestamp without time zone,
name character varying(255),
enabled boolean,
auto_set boolean,
section character varying(17),
input_type character varying(8),
user_id bigint,
CONSTRAINT pk_field PRIMARY KEY (id),
CONSTRAINT fk_field_user_3 FOREIGN KEY (user_id)
REFERENCES account (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT ck_field_input_type CHECK (input_type::text = ANY (ARRAY['TEXT'::character varying, 'TEXTAREA'::character varying]::text[])),
CONSTRAINT ck_field_section CHECK (section::text = ANY (ARRAY['MAIN_CONTACT_INFO'::character varying, 'PARTICIPANT_INFO'::character varying]::text[]))
);
CREATE INDEX ix_field_user_3
ON field
USING btree
(user_id);
There is no column default defined for field.id. Since the sequence public.field_seq seems to exist already (but is not attached to field.id) you can fix it with:
ALTER SEQUENCE field_seq OWNED BY field.id;
ALTER TABLE field
ALTER COLUMN id SET DEFAULT (nextval('field_seq'::regclass));
Make sure the sequence isn't in use for something else, though.
It would be much simpler to create your table like this to begin with:
CREATE TABLE field
(
id bigserial PRIMARY KEY,
...
);
Details on serial or bigserial in the manual.
Not sure how the the Play framework implements this.
This works.
insert into field (id, name) values (nextval('field_seq'), "Me");

CMP 2.0 bean auto-generated primary key WAS 6.1

is it possible to map bean's key field with identity primary key column in DB2?
Sample table:
CREATE TABLE ADDRESS (
ID INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 2147483647
NO CYCLE
CACHE 20
NO ORDER ),
Line1 VARCHAR(255) NOT NULL,
Line2 VARCHAR(255),
City VARCHAR(255) NOT NULL,
Postcode VARCHAR(6) NOT NULL,
Country VARCHAR(50) NOT NULL,
Latitude DOUBLE,
Longitude DOUBLE
)
AUDIT NONE
DATA CAPTURE NONE
CCSID UNICODE;
ejbCreate methods have been tailored NOT TO set ID field, but it gets initialized with default for integer type - 0 so i'm getting DuplicateKeyException on second and following calls to ejbCreate.
What is the best way to implement IDENTITY behavior? I found many examples for JBoss but nothing for WAS.
It was easy with JPA, but CMP 2.0 is a must at this time
Override method ejbPostCreate. You will be able to retrieve the generated ID from there, and update your model and your code in order to avoid duplicate IDs.
For instance, take a look at http://forums.sun.com/thread.jspa?threadID=699131

Categories

Resources