I'm having an error when I'm creating my in-memory database.
When I start up my program on my Tomcat Server, it seems to run fine, until I try and insert into the database.
My Tomcat log reveals the following:
Hibernate: alter table BrokerReferrerJoin drop constraint FK_cfc92321040c4c7e94cd7d7f0f0
Hibernate: alter table BrokerTable drop constraint FK_85c86a6cd9d14a23840e4746545
Hibernate: alter table LeadTable drop constraint FK_26c301acde8d4daa93734bda8ff
Hibernate: alter table LeadTable drop constraint FK_d358f93455874bcba7ed6afd996
Hibernate: alter table ReferrerBrokerJoin drop constraint FK_60dc4e2e6a44406a8fdf4e869e8
Hibernate: alter table ReferrerTable drop constraint FK_910ad4a791e9434da73fbc24ba5
Hibernate: drop table BrokerReferrerJoin if exists
Hibernate: drop table BrokerTable if exists
Hibernate: drop table LeadTable if exists
Hibernate: drop table Person if exists
Hibernate: drop table ReferrerBrokerJoin if exists
Hibernate: drop table ReferrerTable if exists
Hibernate: alter table BrokerReferrerJoin drop constraint FK_7100b66fe58043ed9fcba0c116c
Hibernate: alter table BrokerTable drop constraint FK_b5a6bbb0789c46b39597c4eb788
Hibernate: alter table LeadTable drop constraint FK_e19293e489d2443f80cef09bf10
Hibernate: alter table LeadTable drop constraint FK_0a56acd9932f4f4f930f1b4c23b
Hibernate: alter table ReferrerBrokerJoin drop constraint FK_c700fee7563d461fb33336901e7
Hibernate: alter table ReferrerTable drop constraint FK_c449adf2fb9e4f4d985c944d852
Hibernate: drop table BrokerReferrerJoin if exists
Hibernate: drop table BrokerTable if exists
Hibernate: drop table LeadTable if exists
Hibernate: drop table Person if exists
Hibernate: drop table ReferrerBrokerJoin if exists
Hibernate: drop table ReferrerTable if exists
Hibernate: create table BrokerReferrerJoin (FlexRowIDLink varchar(255) not null, InternalReferrerIDLink varchar(255) not null)
Hibernate: create table BrokerTable (AdminID varchar(255), isAdmin boolean, FlexRowID varchar(255) not null, primary key (FlexRowID))
Hibernate: create table LeadTable (LeadID varchar(255) generated by default as identity (start with 1), Amount double, FlexRowID varchar(255), LeadEmail varchar(50), LeadFName varchar(50) not null, HPhoneNo integer, LeadLName varchar(50) not null, MPhoneNo bigint, Notes varchar(4095), WPhoneNo integer, Status varchar(255), LoanType varchar(255), leadBroker_FlexRowID varchar(255), leadReferrer_FlexRowID varchar(255), primary key (LeadID))
Hibernate: create table Person (FlexRowID varchar(255) not null, CreatedOn date, Email varchar(255), FName varchar(255), LName varchar(255), LastUpdated date, OrgID varchar(255), OrgName varchar(255), Username varchar(255), primary key (FlexRowID))
Hibernate: create table ReferrerBrokerJoin (InternalReferrerIDLink varchar(255) not null, FlexRowIDLink varchar(255) not null)
Hibernate: create table ReferrerTable (LTSRowID varchar(255), FlexRowID varchar(255) not null, primary key (FlexRowID))
Hibernate: alter table BrokerReferrerJoin add constraint UK_bc91f298149c4420bc453811411 unique (InternalReferrerIDLink)
Hibernate: alter table BrokerReferrerJoin add constraint FK_7100b66fe58043ed9fcba0c116c foreign key (FlexRowIDLink) references BrokerTable
Hibernate: alter table BrokerTable add constraint FK_b5a6bbb0789c46b39597c4eb788 foreign key (FlexRowID) references Person
Hibernate: alter table LeadTable add constraint FK_e19293e489d2443f80cef09bf10 foreign key (leadBroker_FlexRowID) references BrokerTable
Hibernate: alter table LeadTable add constraint FK_0a56acd9932f4f4f930f1b4c23b foreign key (leadReferrer_FlexRowID) references ReferrerTable
Hibernate: alter table ReferrerBrokerJoin add constraint UK_836e448745504545aaa39709e22 unique (FlexRowIDLink)
Hibernate: alter table ReferrerBrokerJoin add constraint FK_c700fee7563d461fb33336901e7 foreign key (FlexRowIDLink) references BrokerTable
Hibernate: alter table ReferrerTable add constraint UK_b0c668bb487b4a1d8b5f7b9657b unique (LTSRowID)
Hibernate: alter table ReferrerTable add constraint FK_c449adf2fb9e4f4d985c944d852 foreign key (FlexRowID) references Person
18:52:45.573 [http-bio-8080-exec-3] ERROR com.au.curtin.Lead.HibernateLeadDao - The sessionFactory was set: org.springframework.orm.hibernate4.LocalSessionFactoryBean#6c80d028
18:52:45.592 [http-bio-8080-exec-3] ERROR com.au.curtin.Lead.LeadServiceClass - The HibernateLeadDao was set: com.au.curtin.Lead.HibernateLeadDao#1c90a278
18:52:49.862 [http-bio-8080-exec-7] ERROR com.au.curtin.LoginController - Starting logger
18:52:54.253 [http-bio-8080-exec-9] ERROR com.au.curtin.LeadController - Returning Referrer Home View
18:53:01.035 [http-bio-8080-exec-10] ERROR com.au.curtin.LeadController - org.springframework.validation.BeanPropertyBindingResult: 0 errors
18:53:01.035 [http-bio-8080-exec-10] ERROR com.au.curtin.Lead.LeadServiceClass - Home Phone Number: 94572707Work Phone Number : nullMobile Phone Number: nullEmail :
18:53:01.098 [http-bio-8080-exec-10] ERROR com.au.curtin.Lead.LeadServiceClass - com.au.curtin.Lead.HibernateLeadDao#1c90a278
18:53:01.098 [http-bio-8080-exec-10] ERROR com.au.curtin.Lead.HibernateLeadDao - Lead : Lead [leadID=null, flexRowID=null, leadFirstName=James, leadLastName=Massey, leadHomePhoneNumber=94572707, leadWorkPhoneNumber=null, leadMobilePhoneNumber=null, borrowingAmount=1000.0, leadEmail=, loanType=null, leadNotes=, leadBroker=null, leadReferrer=null, loanStatus=null]Session is: SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])
Hibernate: insert into LeadTable (LeadID, Amount, FlexRowID, leadBroker_FlexRowID, LeadEmail, LeadFName, HPhoneNo, LeadLName, MPhoneNo, Notes, leadReferrer_FlexRowID, WPhoneNo, Status, LoanType) values (default, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
I don't know why it is suddenly causing this error.
All the fields are set with annotations.
Here is my Spring .xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<tx:annotation-driven transaction-manager="transactionManager" />
<jdbc:embedded-database id="LTSDatabase" type="HSQL" />
<context:component-scan base-package="x.y" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="LTSDatabase" />
<property name="annotatedClasses">
<list>
<value>x.y.Lead</value>
<value>x.y.Broker</value>
<value>x.y.Referrer</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="PersistenceAnnotationPostProcessor"
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="hibernateExceptionTranslator"
class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" />
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"
id="PersistenceExceptionTranslator" depends-on="hibernateExceptionTranslator" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
As you can see I'm using annotations for setting up the database, and for my transactions.
I don't know why this error is occurring. If anyone knows why this is happening, please tell me.
I'm using Spring 3, Hibernate 4.2, and Tomcat 7 if that helps.
Just in case this helps anyone, my problem was that I was trying to make my field that was going to be my primary key declared as a String. Hibernate can't handle a non-numerical auto-generated primary key.
Related
Currently, I'm using:
Spring boot 2.2.2
Spring jdbc 2.5.0
Spring core 2.5.0
Sometimes, I got errors when accessing my endpoints, here is the stack trace, this happen after upgrading my library from:
spring boot 2.1.7
spring jdbc 2.1.8
spring core 2.1.8
Handle DataIntegrityViolationException: PreparedStatementCallback;
SQL [INSERT INTO SPRING_SESSION_ATTRIBUTES(SESSION_PRIMARY_ID, ATTRIBUTE_NAME, ATTRIBUTE_BYTES) SELECT PRIMARY_ID, ?, ? FROM SPRING_SESSION WHERE SESSION_ID = ?];
ERROR: duplicate key value violates unique constraint \"spring_session_attributes_pk\"
Detail: Key (session_primary_id, attribute_name)=(a9427ef5-92a4-4845-8769-e034c3b50b70, SPRING_SECURITY_CONTEXT) already exists.; nested exception is org.postgresql.util.PSQLException:
ERROR: duplicate key value violates unique constraint \"spring_session_attributes_pk\"
Detail: Key (session_primary_id, attribute_name)=(a9427ef5-92a4-4845-8769-e034c3b50b70, SPRING_SECURITY_CONTEXT) already exists.
org.springframework.dao.DuplicateKeyException: PreparedStatementCallback; SQL [INSERT INTO SPRING_SESSION_ATTRIBUTES(SESSION_PRIMARY_ID, ATTRIBUTE_NAME, ATTRIBUTE_BYTES) SELECT PRIMARY_ID, ?, ? FROM SPRING_SESSION WHERE SESSION_ID = ?];
ERROR: duplicate key value violates unique constraint \"spring_session_attributes_pk\"
Detail: Key (session_primary_id, attribute_name)=(a9427ef5-92a4-4845-8769-e034c3b50b70, SPRING_SECURITY_CONTEXT) already exists.; nested exception is org.postgresql.util.PSQLException:
ERROR: duplicate key value violates unique constraint \"spring_session_attributes_pk\"
Detail: Key (session_primary_id, attribute_name)=(a9427ef5-92a4-4845-8769-e034c3b50b70, SPRING_SECURITY_CONTEXT) already exists.
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:243)
Here is my session tables:
CREATE TABLE APP_SPRING_SESSION (
PRIMARY_ID CHAR(36) NOT NULL,
SESSION_ID CHAR(36) NOT NULL,
CREATION_TIME BIGINT NOT NULL,
LAST_ACCESS_TIME BIGINT NOT NULL,
MAX_INACTIVE_INTERVAL INT NOT NULL,
EXPIRY_TIME BIGINT NOT NULL,
PRINCIPAL_NAME VARCHAR(100),
CONSTRAINT APP_SPRING_SESSION_PK PRIMARY KEY (PRIMARY_ID)
);
CREATE UNIQUE INDEX APP_SPRING_SESSION_IX1 ON APP_SPRING_SESSION (SESSION_ID);
CREATE INDEX APP_SPRING_SESSION_IX2 ON APP_SPRING_SESSION (EXPIRY_TIME);
CREATE INDEX APP_SPRING_SESSION_IX3 ON APP_SPRING_SESSION (PRINCIPAL_NAME);
CREATE TABLE APP_SPRING_SESSION_ATTRIBUTES (
SESSION_PRIMARY_ID CHAR(36) NOT NULL,
ATTRIBUTE_NAME VARCHAR(200) NOT NULL,
ATTRIBUTE_BYTES BYTEA NOT NULL,
CONSTRAINT APP_SPRING_SESSION_ATTRIBUTES_PK PRIMARY KEY (SESSION_PRIMARY_ID, ATTRIBUTE_NAME),
CONSTRAINT APP_SPRING_SESSION_ATTRIBUTES_FK FOREIGN KEY (SESSION_PRIMARY_ID) REFERENCES APP_SPRING_SESSION(PRIMARY_ID) ON DELETE CASCADE
);
Does anyone know how to fix the problem?
These errors mean that there are already records with such keys in your database.
You can clear the database or change the keys and the Insert queries.
I have a child table in oracle that has two foreign key columns, relating to two different parent tables. I want to create a constraint that says the child must have at least one of those parents - e.g.
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
FOREIGN KEY (column1)
REFERENCES parent_table (column1)
OR
FOREIGN KEY (column2)
REFERENCES parent_table_2 (column1)
This won't work with a foreign key constraint because that can only relate to one parent table - is it possible to do this with a check constraint instead?
Foreign key constraints ensure the referential integrity, not mandatory values.
I think you have to have to separate FK contraints and additional check constraint like this:
alter table table_name
add constraint c_check_cols
check(column1 is not null or column2 is not null);
You can do it with constraints:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name_1
FOREIGN KEY (column1)
REFERENCES parent_table (column1);
ALTER TABLE table_name
ADD CONSTRAINT constraint_name_2
FOREIGN KEY (column2)
REFERENCES parent_table_2 (column1);
ALTER TABLE table_name
ADD CONSTRAINT constraint_name_3
check (COALESCE(column1, column2) IS NOT NULL);
Of course for column1 and column2 column you must permit NULL values.
I have one simple table:
-- Create table
create table FAVOURITE_RULES
( rule_id NUMBER(9) not null,
user_id NUMBER(9) not null);
-- Create/Recreate primary, unique and foreign key constraints
alter table FAVOURITE_RULES
add constraint FAV_RULES_PK primary key (RULE_ID, USER_ID)
alter table FAVOURITE_RULES
add constraint FAV_RULES_RULE_ID_FK foreign key (RULE_ID)
references RULES (RULE_ID) on delete cascade;
alter table FAVOURITE_RULES
add constraint FAV_RULES_USER_ID foreign key (USER_ID)
references USER_AUTHENTICATION (USER_ID) on delete cascade;
I have a rule (from .Net code) that doesn't exist in the original table RULES. It has the Id=-999.
When I try to insert into the FAVOURITE_RULES I get an error about integrity constraint violation (as expected) (FAV_RULES_RULE_ID_FK) violated - parent key not found.
Can I keep the foreign key (FAV_RULES_RULE_ID_FK ) and allow extra only this value (-999) to be inserted?
May be this can help.
Step 1: drop fk constraint
Step 2: insert your violating row
Step 3: again create fk constraint with ENABLE NOVALIDATE
I have a table with around 3,00,000 records, like
CREATE TABLE xxx
( xxx_PK NUMBER(10,0),
CREATEDTIME TIMESTAMP (6),
MODIFIEDTIME TIMESTAMP (6),
CREATOR NUMBER(10,0),
LASTMODIFIER_FK NUMBER(10,0),
a_FK NUMBER(10,0),
b_FK NUMBER(10,0),
c_FK NUMBER(10,0),
d_FK NUMBER(10,0),
e_FK NUMBER(10,0),
f NUMBER(10,0),
PRIMARY KEY (xxx_PK),
CONSTRAINT FOREIGN KEY (LASTMODIFIER_FK)
REFERENCES USERS (USERID_PK) ENABLE NOVALIDATE,
CONSTRAINT FOREIGN KEY (a_FK)
REFERENCES a (a_PK) ENABLE NOVALIDATE,
CONSTRAINT FOREIGN KEY (b_FK)
REFERENCES b (b_PK) ENABLE NOVALIDATE,
CONSTRAINT FOREIGN KEY (c_FK)
REFERENCES c (c_PK) ENABLE NOVALIDATE,
CONSTRAINT FOREIGN KEY (d_FK)
REFERENCES d (d_PK) ENABLE NOVALIDATE,
CONSTRAINT FOREIGN KEY (e_FK)
REFERENCES e (e_PK) ENABLE NOVALIDATE
);
CREATE INDEX f_INDEX ON xxx (f) ;
When I am trying to truncate, getting error -
ora 02266
02266, 00000, "unique/primary keys in table referenced by enabled foreign keys"
*Cause: An attempt was made to truncate a table with unique or
primary keys referenced by foreign keys enabled in another table.
Other operations not allowed are dropping/truncating a partition of a
partitioned table or an ALTER TABLE EXCHANGE PARTITION.
*Action: Before performing the above operations the table, disable the
foreign key constraints in other tables. You can see what
constraints are referencing a table by issuing the following
command:
SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = "tabnam";
We also table yyy and zzz which contains reference of table xxx. Table yyy and zzz is empty.
EDIT -
When I disabled constraint of his child table yyy and zzz, it was successfully truncated.
Please suggest what should I do.
You need Foreign Keys with cascade delete
in linked tables.
I have two tables
Parent table
(account_number varchar(15) not null,
branch_name varchar(50) not null,
balance number not null,
primary key(account_number));
Child table
account_number varchar(15) not null,
foreign key(account_number) references parent table(account_number));
I am trying this:
DELETE FROM parent table
WHERE balances > 1000;
I am deleting accounts by balances on the parent but I get an error message about the child relationship.
My assumption is a DELETE CASCADE has to be added to the foreign key in the child table. All the documentation shows how to alter the table when the constraint is named. I do not have that situation. Is there a way to do it, or do I have to specify the cascade in the delete statement I am writing?
Every constraint in Oracle has a name. If a name isn't specified when the constraint is created, Oracle will autogenerate a name for the constraint. If you don't know what the name of a constraint is, try running a SQL statement that violates the constraint and reading the constraint name from the error message:
SQL> delete from parent where account_number = 1234;
delete from parent where account_number = 1234
*
ERROR at line 1:
ORA-02292: integrity constraint (LUKE.SYS_C007357) violated - child record
found
In this case the name of the constraint is SYS_C007357.
If that doesn't work, you can query the data dictionary view user_constraints:
SQL> select constraint_name from user_constraints where table_name = 'CHILD' and constraint_type = 'R';
CONSTRAINT_NAME
------------------------------
SYS_C007357
As far as I can tell, you can't modify a foreign key constraint to enable ON DELETE CASCADE. Instead you must drop the constraint and recreate it.
I don't believe you can apply the CASCADE option to a DELETE statement either, but you can delete the child rows before deleting from the parent:
DELETE FROM child
WHERE account_number IN (SELECT account_number FROM parent WHERE balance > 1000);
DELETE FROM parent
WHERE balance > 1000;
However, I don't know how many other tables you have with foreign key constraints referencing your parent table, nor in how many places you are deleting from the parent table, so I can't say how much work it would be to use this approach.
yes you can set DELETE CASCADE
see more info here FOREIGN KEYS WITH CASCADE DELETE
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
ON DELETE CASCADE
);
for example
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE CASCADE
);