0 Replies Latest reply on May 1, 2012 1:47 PM by jcf

    Foreign Key as Primary Key and Cascaded persistence

    jcf

      Hi all,

       

      I have problem with cascading persistence with the following code consisting of 2 annotated classes Listing and ListingLocation.

      if a Listing instance is set to the listing2 of a ListingLocation instance, the Listing instance will be automatically persisted when I save the ListingLocation instance

      if a Listing instance is only set to the listing1 of a ListingLocation instance, the Listing instance will NOT be automatically persisted when I save the ListingLocation instance

      listing1 is also used as the primary and foreign key.

       

      Would any one please tell me how should I code to get listing1 to automatically get persisted?

       

      Thank you very much.

       

      --code--

      @Entity

      @Table

      @Access(AccessType.FIELD)

      public class Listing implements Serializable {

      @Id

      @GeneratedValue

      private long id;

      @Column(columnDefinition = "DECIMAL(10,2)")

      private double price;

      ...}


      @Entity

      @Table

      @Access(AccessType.FIELD)

      public class ListingLocation implements Serializable {

        @Id

                @OneToOne()

                @Cascade(value={ CascadeType.PERSIST, CascadeType.SAVE_UPDATE })

                @ForeignKey(name = "FK_ID")

                @JoinColumn(name = "ID", referencedColumnName = "id", unique = true, updatable = false, insertable = false)

                private Listing listing1;

            @Column(name = "address")

                private String address;

            @OneToOne()

                @Cascade(value={ CascadeType.PERSIST, CascadeType.SAVE_UPDATE })

                @JoinColumn(name = "List2", referencedColumnName = "id", nullable = true)

                private Listing listing2;

      ...}