7 Replies Latest reply on Jul 8, 2006 2:22 PM by pmuir

    the bundled hibernate doesn't work @oneToOne shared primary

    liudan2005

      Here is my code:

      Owner{
      @Id
      public Long getId() { return id; }
      
      @OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
      @PrimaryKeyJoinColumn
       public Employment getEmployment() { return employment; }
      }
      
      Employment {
       @Id
       @GenericGenerator(name="fk", strategy="foreign", parameters={
       @Parameter(name="property", value="owner")
       })
       @GeneratedValue(strategy=GenerationType.AUTO, generator="fk")
       @Column(name="owner_id")
       public Long getId(){ return id; }
      
       @OneToOne(mappedBy="employment")
       public AccountOwner getOwner(){ return owner; }
      }
      
      


      Here is the error message I get:
      Unknown mappedBy in: Employment.owner, referenced property unknown: Owner.employment
      


      Any idea?

        • 1. Re: the bundled hibernate doesn't work @oneToOne shared prim
          pmuir

          I suggest you ask in the EJB3 or hibernate forum. This problem is nothing to do with Seam.

          • 2. Re: the bundled hibernate doesn't work @oneToOne shared prim
            liudan2005

            The trouble I've been asking many questions in Ejb3 forum but never got any reply. That's why I am trying my luck here. Thanks.

            • 3. Re: the bundled hibernate doesn't work @oneToOne shared prim
              pmuir

              Can you post all your code including the AccountOwner class. It would also be easier to help if you simplyfy your problem to as basic a class as possible BUT include all the code in the class.

              • 4. Re: the bundled hibernate doesn't work @oneToOne shared prim
                liudan2005

                Here is the code I've tried which doesn't work:

                @Entity
                Owner{
                private Long id;
                private Employment employment;
                @Id
                public Long getId() {
                return id;
                }
                
                public Long setId(Long id) {
                this.id=id;
                }
                
                @OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
                @PrimaryKeyJoinColumn
                public Employment getEmployment() {
                return employment;
                }
                }
                
                @Entity
                Employment {
                 private Long id;
                 private Owner owner;
                 @Id
                 @GenericGenerator(name="fk", strategy="foreign", parameters={
                 @Parameter(name="property", value="owner")})
                 @GeneratedValue(strategy=GenerationType.AUTO, generator="fk")
                 public Long getId(){ return id; }
                
                 @OneToOne(mappedBy="employment")
                 public AccountOwner getOwner(){ return owner; }
                }
                
                


                What I want is a one to one relationship with shared primary key. Owner and Employment are seperate entities and they are mapped to 2 different tables. Each table would have a id field as primary key. Here is how my testing code would look like:
                Owner owner=new Owner();
                Employment emp=new Employment();
                owner.setEmployment(emp);
                em.persist(owner); //both owner and emp are saved here and ids are generated automatically.
                


                Basically, I want ids are generated automatically for owner, and same id is set for Employment. Is this possible?



                • 5. Re: the bundled hibernate doesn't work @oneToOne shared prim
                  pmuir

                  There is a lot of discussion on the hibernate ejb forum about this. I did try something similar a few months ago and couldn't get it to work. But, looking at the hibernate annotations test suite there are tests which show it working - take a look, copy the example that seems most like what you want (probably Party/PartyAffliate) and modify as required.

                  • 6. Re: the bundled hibernate doesn't work @oneToOne shared prim
                    liudan2005

                    I've looked into them, but non of them actually generates Id automatically. I have to persist one entity first and then retrive the Id from first saved entity and pass it to second entity manually.

                    I guess this is a limitation in Hibernate that you can't use generated Ids when shared primary key is used.

                    Thanks anyway.

                    • 7. Re: the bundled hibernate doesn't work @oneToOne shared prim
                      pmuir

                      Ok, I see what you mean; I agree, I think this is something that should work.

                      Looking at what I wrote I used @PostPersist to set the Id on OneToOne relationships, but then I don't seem to have cascaded the persist so I'm not sure if that will work in this case.