Hi everyone
thanks for reading this post. I observed a tricky point about hibernate and my database. Basicly, hibernate seems to re-create the foreign keys I previously defined in the database.
Hibernate version: 3.2.4
Mapping documents:
Code:
package org.cnio.appform.entity;
import static javax.persistence.GenerationType.SEQUENCE;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
/**
* RelGrpAppusr entity.
*/
@Entity
@Table(name = "rel_grp_appusr")
public class RelGrpAppuser implements java.io.Serializable {
// Fields
@Id
@Column(name="idgrp_usr")
@SequenceGenerator(sequenceName="rel_grp_appusr_idgrp_usr_seq", name = "GroupUsrSeqGen")
@GeneratedValue(generator="GroupUsrSeqGen", strategy = SEQUENCE)
private Integer id;
@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumn(name = "codgroup", unique = false, nullable = false, insertable = true, updatable = true)
private AppGroup appgroup;
@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumn(name = "coduser", unique = false, nullable = false, insertable = true, updatable = true)
private AppUser appuser;
// Constructors
/** default constructor */
public RelGrpAppuser() {
}
/** full constructor */
public RelGrpAppuser(AppGroup appgroup, AppUser appuser) {
this.appgroup = appgroup;
this.appuser = appuser;
appgroup.getRelGrpAppusrs().add(this);
appuser.getRelGrpAppusrs().add(this);
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer idgrpUsr) {
this.id = idgrpUsr;
}
public AppGroup getAppgroup() {
return this.appgroup;
}
public void setAppgroup(AppGroup appgroup) {
if (appgroup != null) {
this.appgroup = appgroup;
appgroup.getRelGrpAppusrs().add(this);
}
}
public AppUser getAppuser() {
return this.appuser;
}
public void setAppuser(AppUser appuser) {
if (appuser != null) {
this.appuser = appuser;
appuser.getRelGrpAppusrs().add(this);
}
}
}
Name and version of the database you are using: PostgreSQL 8.1.4
The generated SQL (show_sql=true):Well, this is not the some generated SQL, is the the DDL I wrote to define the table (and, following below, the DDL after executing the application the very first time):
Code:
CREATE TABLE "public"."rel_grp_appusr" (
"idgrp_usr" SERIAL,
"codgroup" INTEGER NOT NULL,
"coduser" INTEGER NOT NULL,
CONSTRAINT "pk_rel_grp_appusr" PRIMARY KEY("idgrp_usr"),
CONSTRAINT "fk_rel_grp__rel_grp_a_appgroup" FOREIGN KEY ("codgroup")
REFERENCES "public"."appgroup"("idgroup")
ON DELETE CASCADE
ON UPDATE CASCADE
NOT DEFERRABLE,
CONSTRAINT "fk_rel_grp__rel_grp_a_appuser" FOREIGN KEY ("coduser")
REFERENCES "public"."appuser"("iduser")
ON DELETE CASCADE
ON UPDATE CASCADE
NOT DEFERRABLE
) WITHOUT OIDS;
This is a table implementing a M:N relationship.
After running my (standalone) hibernate application test for the very first time after creating the database, the DDL for this table gets into this:
Code:
CREATE TABLE "public"."rel_grp_appusr" (
"idgrp_usr" SERIAL,
"codgroup" INTEGER NOT NULL,
"coduser" INTEGER NOT NULL,
CONSTRAINT "pk_rel_grp_appusr" PRIMARY KEY("idgrp_usr"),
CONSTRAINT "fk3e240fd3289fe600" FOREIGN KEY ("coduser")
REFERENCES "public"."appuser"("iduser")
ON DELETE NO ACTION
ON UPDATE NO ACTION
NOT DEFERRABLE,
CONSTRAINT "fk3e240fd3e9d1abd4" FOREIGN KEY ("codgroup")
REFERENCES "public"."appgroup"("idgroup")
ON DELETE NO ACTION
ON UPDATE NO ACTION
NOT DEFERRABLE,
CONSTRAINT "fk_rel_grp__rel_grp_a_appgroup" FOREIGN KEY ("codgroup")
REFERENCES "public"."appgroup"("idgroup")
ON DELETE CASCADE
ON UPDATE CASCADE
NOT DEFERRABLE,
CONSTRAINT "fk_rel_grp__rel_grp_a_appuser" FOREIGN KEY ("coduser")
REFERENCES "public"."appuser"("iduser")
ON DELETE CASCADE
ON UPDATE CASCADE
NOT DEFERRABLE
) WITHOUT OIDS;
Well, the same happens to the rest of the tables in the database.
Am I doing anything wrong? Is this a know hibernate functionality?
My application worked fine but, as i am doing improvements, changes or, simply, fixing bugs, things are getting down.
i would like some hint or idea, please.
Thanks in advance
willy