3 Replies Latest reply on Sep 14, 2007 10:45 AM by waynebaylor

    How to map this?

    lafr

      I'm migrating an EJB 2.1 application to EJB 3.0 using JBoss 4.2.2.GA.
      Some question on how to map fields and relations.
      Object message contains a message header, message parts and each message part contains message lines.
      message head has a generated id.
      message part has a composite pk build of message head id and part_no.

      @Entity
      @Table(name="head")
      public class Head implements Serializable
      {
       @Id
       @GeneratedValue(strategy=GenerationType.IDENTITY)
       @Column(name="head_id")
       private Integer headId;
      
       @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
       @OrderBy("partNo")
       @JoinColumns({
       @JoinColumn(name="head_id",referencedColumnName="head_id")
       })
       private Collection<Part> parts = new ArrayList<Part>();
      

      This gives me an Exception saying
      Repeated column in mapping for entity: Head column: head_id (should be mapped with insert="false" update="false").

      Is adding 'insert="false" update="false"' to the JoinColumn properties the right way to solve this duplicate field?
      What about using Transient?

      I get similar problems with ManyToOne relations.
      Database table for Order has a field order_type.
      This field is a FK to the OrderType table id.
      But order_type in combination with output_type is a composite FK to OrderProperties.
      So I would have in Order.java:
      - a field of type Integer named orderType
      - a field of type Integer named outputType
      - a ManyToOne relation to EB OrderType with JoinColumn orderType
      - a ManyToOne relation to EB OrderProperties with JoinColumns orderType and outputType
      So three mappings for column order_type and two mapping for column output_type.
      Should I omit the plain fields in this case and mark the JoinColumn orderType for OrderProperties with 'insert="false" update="false"' then?
      Or what's the best practice for something like this?

        • 1. Re: How to map this?
          waynebaylor

          is this a bi-directional relationship? can you post the Part entity code?

          • 2. Re: How to map this?
            lafr

            No, it's uni-directional.
            So in the part bean I have the key-fields marked with id and so on. No hint that part is used in a relation.

            @Entity
            @IdClass(value=Part.PK.class)
            @Table(name="part")
            public class Part implements Serializable
            {
             @Id
             @Column(name="msghd_serial")
             private Integer msghdSerial;
            
             @Id
             @Column(name="part_no")
             private Integer partNo;
             ...
            



            • 3. Re: How to map this?
              waynebaylor

              hmm, when using @OneToMany and @JoinColumn Hibernate puts the FK in the Part table...so you shouldn't have a conflict. Maybe try removing the referencedColumnName attribute and see what happens.