-2

i'm using hibernate annotations in my project, I've created the tables and it's all good except that when i check the database their's no cascade even tho I've made sure to put it in the classes. here is an exemple of how i do it :

public class Item implements java.io.Serializable {

/**
 *
 */
private static final long serialVersionUID = 8271695210797279161L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "IDitem", unique = true, nullable = false)
private int iditem;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "IDDIVISION", nullable = false)
private Division division;
@Column(name = "SIGLE", length = 254)
private String sigleItem;
@Column(name = "DESCRIPTION", length = 254)
private String description;
....
}

i have cascade = CascadeType.ALL in all the ManyToOne, OneToMany and ManyToMany cases. i tried also to add

@OnDelete(action = OnDeleteAction.CASCADE)

@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)

like mentioned here but nothing changed

can you help me solve the problem? thank you!

Community
  • 1
  • 1
nouraty
  • 156
  • 1
  • 14

2 Answers2

1

I had the same problem. Maybe you forget to add the child to the set of the parent.

division.getItems().add(item); // <-- That was my mistake.
item.setDivision(division);
Floaz
  • 140
  • 1
  • 5
0

I think the problem is that you don't use the correct database engine. You should specify hibernate dialect as: org.hibernate.dialect.MySQLInnoDBDialect instead of MySQLDialect. Otherwise, it will be default to MyISAM, which doesn't support relation.

Khue Vu
  • 3,112
  • 4
  • 37
  • 40
  • i changed it but it stated to give errors like août 10, 2012 2:36:47 PM org.hibernate.util.JDBCExceptionReporter logWarnings Avertissement: Unknown table 'participants_audit' août 10, 2012 2:36:47 PM org.hibernate.util.JDBCExceptionReporter logWarnings Avertissement: SQL Warning: 1051, SQLState: 42S02 août 10, 2012 2:36:47 PM org.hibernate.util.JDBCExceptionReporter logWarnings Avertissement: Unknown table 'quest_ecarts' ... – nouraty Aug 10 '12 at 14:37
  • Do you have Entity and Table annotation for the class. Like @Table(name = 'participants_audit'). Otherwise, does it error only happen after you change the Hibernate Dialect ? – Khue Vu Aug 10 '12 at 14:58
  • yes it only happens when i change to MySQLInnoDBDialect and yes i have in each class the anotation @Entity @Table(name = "name") – nouraty Aug 10 '12 at 16:39
  • 1
    Could you manually change your database engine to InnoDB to see if this error still occur ? – Khue Vu Aug 10 '12 at 16:54
  • chenged it to inoobd but tables are still not created, i get: ... Avertissement: SQL Warning: 1051, SQLState: 42S02 août 10, 2012 6:07:23 PM org.hibernate.util.JDBCExceptionReporter logWarnings Avertissement: Unknown table 'division' – nouraty Aug 10 '12 at 18:11
  • and ... Grave: Unsuccessful: create table question (idQuestion integer not null auto_increment unique, text varchar(254), commentaire varchar(254), them_id integer, primary key (idQuestion)) type=InnoDB août 10, 2012 6:07:23 PM org.hibernate.tool.hbm2ddl.SchemaExport create – nouraty Aug 10 '12 at 18:13
  • So you don't even have the table created? Have you set "hibernate.hbm2ddl.auto" to update. It will maintain your schema to be consistent with your model . – Khue Vu Aug 11 '12 at 01:30
  • 1
    no the tables are not created when i change MySQLInnoDBDialect, for the hibernate.hbm2ddl.auto i set it to create (created a new database) – nouraty Aug 11 '12 at 18:01
  • In the question you say the tables exist. Now you're saying they don't. It is impossible to help when you provide misleading information. -1 on the question. – Bill Rosmus Aug 12 '12 at 18:45
  • i said i already created the tables but their is no cascad, then Khue Vu suggested that i change the database engine and try again so offcorse i'm going to try and create tables all over again with his suggestion and that's when i said that it didn't work (changing the engine wouldn't let me even create the tables), hope it's cleared out – nouraty Aug 15 '12 at 09:32