Hi,
Am beginner to hibernate, while i am practicing i found a bug. Following is the description to recreate the bug.
Note: <property name="hbm2ddl.auto">create</property>
1. Create a Address class to store student Address(Attached the Address class). 2. Create a model class for Student in which we will use @ElementCollection annotation to for the Collection to store list of addresses of a student.(Attached Student class version 1). 3. Create a class with main method (Attached CollectionWithHibernate.java) in which create instance for Address Class and store two addresses of a student into Student class instance. 4. write code to save the Student object into the database.
The above steps will create two tables in database 1. Student 2. Student_address Now use @JoinTable(name="StudentAddress") above to the member variable address in Student model class(Attached Student class version 2).Now run the main method to store the Student object
when we run the main method we will get error with the following log
INFO: HHH000227: Running hbm2ddl schema export Hibernate: alter table Student_address StudentAddress drop constraint FKmdtlf7dpg8k2tk4yjr53o37nx FKfwck4fon84bm0xjl8abwgb2hp Hibernate: drop table Student Jan 03, 2016 4: 20 43 : 36 51 PM org.hibernate.tool.hbm2ddl.SchemaExport perform ERROR: HHH000389: Unsuccessful: drop table Student Jan 03, 2016 4: 20 43 : 36 51 PM org.hibernate.tool.hbm2ddl.SchemaExport perform ERROR: Operation 'DROP CONSTRAINT' cannot be performed on object ' SQL160103154527370 SQL160103154615490 ' because CONSTRAINT ' FKFWCK4FON84BM0XJL8ABWGB2HP FKMDTLF7DPG8K2TK4YJR53O37NX ' is dependent on that object. Hibernate: drop table Student_address StudentAddress Hibernate: create table Student (roll integer not null, name varchar(255), primary key (roll)) Jan 03, 2016 4: 20 43 : 36 51 PM org.hibernate.tool.hbm2ddl.SchemaExport perform ERROR: HHH000389: Unsuccessful: create table Student (roll integer not null, name varchar(255), primary key (roll)) Jan 03, 2016 4: 20 43 : 36 51 PM org.hibernate.tool.hbm2ddl.SchemaExport perform ERROR: Table/View 'STUDENT' already exists in Schema 'APP'. Hibernate: create table Student_address StudentAddress (Student_roll integer not null, user_city varchar(255), user_pincode varchar(255), user_state varchar(255), user_street varchar(255)) Hibernate: alter table Student_address StudentAddress add constraint FKmdtlf7dpg8k2tk4yjr53o37nx FKfwck4fon84bm0xjl8abwgb2hp foreign key (Student_roll) references Student Jan 03, 2016 4: 20 43 : 36 51 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: HHH000230: Schema export complete Hibernate: insert into Student (name, roll) values (?, ?) Jan 03, 2016 4: 20 43 : 36 51 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL Error: 20000, SQLState: 23505 Jan 03, 2016 4: 20 43 : 36 51 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by ' SQL160103154527370 SQL160103154615490 ' defined on 'STUDENT'. Jan 03, 2016 4: 20 43 : 36 51 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release INFO: HHH000010: On release of batch it still contained JDBC statements Jan 03, 2016 4: 20 43 : 36 51 PM org.hibernate.internal.SessionImpl$5 mapManagedFlushFailure ERROR: HHH000346: Error during managed flush [could not execute statement] Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute statement
My Analysis: In the Model class Student we have used JoinTable annotation and changed the table name to be created for foreign key(version 2 of Student class). As per hibernate.cfg.cml what ever that are mentioned in <mapping> will be dropped and will be created, so (in Student class version 2) hibernate will try to delete Student table , which will fail because there is foreign key relation between Student and Student_address table. So before deleting a table Student the foreign key relation to the Student_address need to be deleted.
|
|