I'm trying to build a MySQL database gradually, by generation using JPA and Eclipse-Link. Along the way, I've changed some relationships #ManyToOne, #OneToOne etc.
I now have a situation where I have some spurious foreign keys: the tables don't exist, but the referenced tables still do. I think the original tables were cross-reference tables generated by EclipseLink but are no longer around.
The issue is, I cannot delete these referenced tables. I get an error like this:
mysql> drop table PRODUCTDO;
ERROR 3730 (HY000): Cannot drop table 'PRODUCTDO' referenced by a foreign key constraint 'PRODUCTDO_DISTRIBUTIONCENTERPRODUCTDO_ProductDo_ID' on table 'PRODUCTDO_DISTRIBUTIONCENTERPRODUCTDO'.
If I run:
SET foreign_key_checks = 0;
then I can delete the table, but the constraint still remains. Even if I drop the database and create it again, the constraint is still there:
mysql> SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = 'SCO';
+---------------------------------------+------------------------------+----------------------------------------------------+-----------------------------+------------------------+
| TABLE_NAME | COLUMN_NAME | CONSTRAINT_NAME | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME |
+---------------------------------------+------------------------------+----------------------------------------------------+-----------------------------+------------------------+
| PRODUCTDO_DISTRIBUTIONCENTERPRODUCTDO | ProductDo_ID | PRODUCTDO_DISTRIBUTIONCENTERPRODUCTDO_ProductDo_ID | PRODUCTDO | ID |
| PRODUCTDO_DISTRIBUTIONCENTERPRODUCTDO | distributionCenterProduct_ID | PRDCTDDSTRBTIONCENTERPRODUCTDOdstrbtnCntrProductID | DISTRIBUTIONCENTERPRODUCTDO | ID |
+---------------------------------------+------------------------------+----------------------------------------------------+-----------------------------+------------------------+
2 rows in set (0.01 sec)
How can I get rid of these zombie constraints?
In the end, I had to recreate the table and foreign key then I was able to delete it.
Related
I have the following problem, I have a already existing table with three fields field1, field2, field3. Field1 is actually a foreign key (#OneToOne) to another table. All field2 and field3 can be null, so I can't set a primary key for all three fields. In the database there is a UniqueConstraint for field1+field2+field3.
field1 | field2 | field 3
1 | 1 | null
1 | null | 2
3 | 1 | null
I've tried several solutions with JPA/Hibernate but could not find a good one.
Without defining a #Id JPA can not work (of course). Defining the three fields in a #Embeddable and reuse it with #EmbeddedId creates a primary key over all three fields which do not allow null values.
Is there any other solution than change the existing table and add a auto generated id for each row?
Thank you so much & best regards!
Have you tried as below?
#Table(
name="xxx",
uniqueConstraints=
#UniqueConstraint(name="my_unique_key", columnNames={"field1", "field2", "field3"})
)
I have a table like this in Cassandra-
CREATE TABLE DATA_HOLDER (USER_ID TEXT, RECORD_NAME TEXT, RECORD_VALUE BLOB, PRIMARY KEY (USER_ID, RECORD_NAME));
I want to count distinct USER_ID in my above table? Is there any way I can do that?
My Cassandra version is:
[cqlsh 4.1.1 | Cassandra 2.0.10.71 | DSE 4.5.2 | CQL spec 3.1.1 | Thrift protocol 19.39.0]
The select expression is defined as:
selection_list
| DISTINCT selection_list
so you can:
SELECT DISTINCT USER_ID FROM DATA_HOLDER;
I am trying to do a Unidirectional ManyToOne association via a JoinTable in hibernate, but I keep getting the following error:
A JPA error occurred (Unable to build EntityManagerFactory): Unable to find column with logical name: name in org.hibernate.mapping.Table(users) and its related supertables and secondary tables
I have 3 models House, User, UserHouseMap. I want to be able to access a users house through the UserHouseMap. Here is the mapping on the User
For other reasons, I need to map a User to UserHouseMap via a column that is not a primary key
#Id
#GeneratedValue
#Expose
public Long id;
#Expose
#Required
#ManyToOne
#JoinTable(name = "user_house_map",
joinColumns= {#JoinColumn(table="users", name="user_name", referencedColumnName="name")},
inverseJoinColumns={#JoinColumn(table="houses", name="house_name", referencedColumnName="house_name")})
public House house;
Here are the DB schemas for all 3 models
Users
Table "public.users"
Column | Type | Modifiers
-----------------------+-----------------------------+-----------------------------
name | character varying(255) |
id | integer | not null
Indexes:
"user_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"housing_fkey" FOREIGN KEY (name) REFERENCES user_house_map(user_name) DEFERRABLE INITIALLY DEFERRED
Houses
Table "public.houses"
Column | Type | Modifiers
---------------+------------------------+-----------
house_name | character varying(255) | not null
address | text |
city | text |
state | text |
zip | integer |
zip_ext | integer |
phone | text |
Indexes:
"house_pkey" PRIMARY KEY, btree (house_name)
Referenced by:
TABLE "user_house_map" CONSTRAINT "house_map_fkey" FOREIGN KEY (house_name) REFERENCES house(house_name) DEFERRABLE INITIALLY DEFERRED
UserHouseMap
Table "public.user_house_map"
Column | Type | Modifiers
-------------+------------------------+-----------
user_name | character varying(255) | not null
house_name | character varying(255) | not null
Indexes:
"user_house_map_pkey" PRIMARY KEY, btree (user_name)
"user_house_map_house_key" btree (house_name)
Foreign-key constraints:
"user_house_map_house_fkey" FOREIGN KEY (house_name) REFERENCES houses(house_name) DEFERRABLE INITIALLY DEFERRED
Referenced by:
TABLE "users" CONSTRAINT "housing_fkey" FOREIGN KEY (name) REFERENCES user_house_map(user_name) DEFERRABLE INITIALLY DEFERRED
Remove table="users" and table="houses" from the JoinColumn annotations.
This attribute should not contain the target table of the foreign key. It's only used when an entity is mapped to two tables, in order to tell which table the join column is in.
Also, your table definitions are quite strange:
the join table should have two foreign keys: one referencing the PK of the users table, and one referencing the PK of the house table. Why do you reference the name of a user rather than its ID?
the foreign key constraint on users.name doesn't make sense.
I would like to know how to create custom setups/teardown mostly to fix cyclyc refence issues where I can insert custom SQL commands with Spring Test Dbunit http://springtestdbunit.github.io/spring-test-dbunit/index.html.
Is there an annotation I can use or how can this be customized?
There isn't currently an annotation that you can use but you might be able to create a subclass of DbUnitTestExecutionListener and add custom logic in the beforeTestMethod. Alternatively you might get away with creating your own TestExecutionListener and just ordering it before DbUnitTestExecutionListener.
Another, potentially better solution would be to re-design your database to remove the cycle. You could probably drop the reference from company to company_config and add a unique index to company_id in the company_config table:
+------------+ 1 0..1 +--------------------------------+
| company |<---------| company_config |
+------------+ +--------------------------------+
| company_id | | config_id |
| ... | | company_id (fk, notnull, uniq) |
+------------+ +--------------------------------+
Rather than looking at company.config_id to get the config you would do select * from company_config where company_id = :id.
Dbunit needs the insert statements (xml lines) in order, because they are performed sequentially. There is no or magic parameter or annotation so dbunit can resolve your cyclyc refences or foreign keys automatically.
The most automate way I could achieve if you your data set contain many tables with foreign keys:
Populate your database with few records. In your example: Company, CompanyConfig and make it sure that the foreign keys are met.
Extract a sample of your database using dbunit Export tool.
This is an snippets you could use:
IDatabaseConnection connection = new DatabaseConnection(conn, schema);
configConnection((DatabaseConnection) connection);
// dependent tables database export: export table X and all tables that have a // PK which is a FK on X, in the right order for insertion
String[] depTableNames = TablesDependencyHelper.getAllDependentTables(connection, "company");
IDataSet depDataset = connection.createDataSet(depTableNames);
FlatXmlWriter datasetWriter = new FlatXmlWriter(new FileOutputStream("target/dependents.xml"));
datasetWriter.write(depDataset);
After running this code, you will have your dbunit data set in "dependents.xml", with all your cycle references fixed.
Here I pasted you the full code: also have a look on dbunit doc about how to export data.
I am running a MySQL 5.1 server and Hibernate 3.5.1 / JPA2 for ORM. Everthing seems fine until I drop some tables manually. From then on, unit tests fail with Hibernate no longer creating certain tables. Changing the jdbc url from
url=jdbc:mysql://localhost:3306/dbjava?createDatabaseIfNotExist=true
to
url=jdbc:mysql://localhost:3306/dbjavanew?createDatabaseIfNotExist=true
solves the problem ... until I perform some table drops :-( Since I am going to use MySQL for production, this must not happen at all.
Dropping dbjava does not help too. Any suggestions?
--- ADDITIONAL INFO:
It's getting really weired. From mysql console:
mysql> use dbjava;
Database changed
mysql> show tables;
+---------------------+
| Tables_in_dbjava |
+---------------------+
| aa |
| ba |
| da |
| ea |
| hibernate_sequences |
| ia |
| ka |
| la |
+---------------------+
8 rows in set (0.00 sec)
mysql> create table `cA` (id integer not null, comment varchar(255), name varchar(255), id_d integer, id_f integer, primary key (id)) ENGINE=InnoDB;
ERROR 1050 (42S01): Table 'ca' already exists
Uh? Why does Table 'ca' exist?!?!? Actually, it once existed ... since then, it was dropped, the whole db was dropped several times ... why does it still exist?
Even worse:
mysql> drop table cA;
mysql> ERROR 1051 (42S02): Unknown table 'ca'
Totally confused ...
However, I just realised something: my table names in hibernate use lower and upper case (camel case) notation. mysql responses lower case only. Would anybody confirm that mysql does not recognize case-sensitiveness in the year 2010 ?!?
Which dialect are you using? Newer versions of Hibernate (can't remember exact numbers) have InnoDB dialect fix: instead of type=InnoDb it uses engine=InnoDb when creating tables -- which is the only legitimate syntax in MySQL 5.1. Just to remind, MySQL 5.0 accepted both type and engine keywords.