Announcement Announcement Module
Collapse
No announcement yet.
Setting a default foreign key with hibernate annotations Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting a default foreign key with hibernate annotations

    Hi everyone,
    I am new to the world of spring and hibernate. I found this forum very interessting, and now I am writing my first post on it

    Also, to my problem. I have a little difficulty setting a default value for a foreign key with hibernate.
    I have one table with a column called "severityID" that points to the "id" of a second table "severity". This column is not nullable and has a default value. on the database this can be performed as follows:
    PHP Code:
    cw_severityId INT NOT NULL default 1
    In order to do the same on hibernate I used the annotation "OneToOne", and the property is set to the class that represent the "severity" table.
    PHP Code:
        @OneToOne
        
    @JoinColumn(name="severityId")
        private 
    Severity severity
    Did someone know how to set a default value to this property? in this example it should point to the first element of the "severity" table.
    If the value is a primitive type that would be easily done with
    PHP Code:
    @Column(name="severityId",nullable=false,columnDefinition="int default 1"
    or
    PHP Code:
    @Column(name="cw_severityId")
        private 
    int severity=1
    Hope that someone can help, this time google couldnīt...
    Last edited by Sakr; Oct 8th, 2009, 02:39 AM.

  • #2
    Hi Buddy,

    I am not sure about other issues in the code you are developing, and this not a problem with Spring.
    This is the technique in Hibernate, You can set default values by setting class property as dynamic-update="true" and dynamic-insert="true".

    Try this out. I think this should work.

    Regards,
    Prabodh

    Comment


    • #3
      Hi Prabodh,

      Thank you for the reply, that was the right way. The problem is solved now.
      I knew that this problem is a hibernate one. But as Iīm using the spring framework, I didnīt want to register in another forum just for this question. And besides I knew that there are a lot of experts here
      Also to the problem again, it is now solved by using explicitly the entity annotation from the hibernate framework and setting the dynamic update and insert properties to "true".
      PHP Code:
      @Entity
      @org.hibernate.annotations.Entity(dynamicUpdate=truedynamicInsert=true)
      @
      Table(name="Element"
      Regards,
      Sakr

      Comment


      • #4
        i am also facing the same problem so that plz help me out!

        Comment


        • #5
          As I already wrote, the problem is resolved. Here are the step that were performed:
          First a default value was set for the foreign key column in the database using the sql query:
          PHP Code:
          severityId INT NOT NULL default 1
          Then you have to set the annotations "dynamicInsert" and "dynamicUpdate" to true in the declaration of your (table-)class:
          PHP Code:
          @Entity
          @org.hibernate.annotations.Entity(dynamicUpdate=truedynamicInsert=true)
          @
          Table(name="Element_Table"
          public class 
          Element implements java.io.Serializable{
          ...

          The last step is to declare your variable of the type of the (table-)class pointed by this foreign key and mention the relationship between the tables:
          PHP Code:
              @OneToOne
              
          @JoinColumn(name="severityId")
              private 
          Severity severity
          Regards,
          Sakr

          Comment

          Working...
          X