-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: re-creation of foreign keys
PostPosted: Tue Dec 16, 2008 7:19 am 
Newbie

Joined: Tue Dec 16, 2008 6:59 am
Posts: 6
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


Last edited by aeiou on Tue Dec 16, 2008 8:01 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2008 7:32 am 
Newbie

Joined: Tue Dec 16, 2008 6:59 am
Posts: 6
Well, i forgot something in the previous post. I got two errors at initialization time from SchemaUpdate:

Code:
ERROR SchemaUpdate.execute:155 - Unsuccessful: alter table rel_grp_appusr add constraint FK3E240FD3BCE294C4 foreign key (codgroup) references appuser
ERROR SchemaUpdate.execute:156 - ERROR: insert or update on table "rel_grp_appusr" violates foreign key constraint "fk3e240fd3bce294c4"
  Detail: Key (codgroup)=(3) is not present in table "appuser".


Obviously, i dont know where that foreign key comes from.

Sorry for the missing.

willy


Top
 Profile  
 
 Post subject: Re: re-creation of foreign keys
PostPosted: Thu Jun 16, 2011 12:17 pm 
Newbie

Joined: Tue Dec 16, 2008 6:59 am
Posts: 6
hmmm?????


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.