4 Replies Latest reply on Sep 14, 2007 12:53 AM by kaviarasu

    Inserting into primary and secondry table using bean

    kaviarasu

      Hi
      i want to insert value for primary and secondry table through bean itself without using trigger,
      can any one help


      table 1
      employee
      eno-primary key
      ename-

      table-2
      eno-foreign key
      address-
      esino-primary key for this table


      i want to write bean that table1.eno is autogenerate value that want to insert into secondry table and the same value want to assigned to table2.esino which is the primary coloumn for table 2.
      is it possible through trigger or
      i want to attain it through bean itself
      thank you
      regards
      kaviarasu

        • 1. Re: Inserting into primary and secondry table using bean
          trickyvail

          This is a question for the EJB3 forum.

          Yes this is easy to do without using a trigger.

          You could map as a One-to-One Unidirectional or a One-to-One Bidirectional.

          e.g. One-to-One Unidirectional

          @Entity
          public class Employee
          {
           private int id;
           private Address address;
          
           @Id
           @Column(name = "eno")
           @GeneratedValue
           public int getId() { return this.id; }
          
           @OneToOne
           @PrimaryKeyJoinColumn
           public Address getAddress() { return address; }
          
          }


          • 2. Re: Inserting into primary and secondry table using bean
            kaviarasu

            hi i cant able to understand ur schema
            i have pasted the sample coding
            can u say how to insert in this, i created two tables Table 1 and table 2. this code is generated by exadel

            @Entity
            public class Table1 implements Serializable {
             @Id
             private BigDecimal no;
            
             @OneToMany(mappedBy="no")
             private Set<Table2> table2Collection;
            
             private static final long serialVersionUID = 1L;
            
             public Table1() {
             super();
             }
            
             public BigDecimal getNo() {
             return this.no;
             }
            
             public void setNo(BigDecimal no) {
             this.no = no;
             }
            
             public Set<Table2> getTable2Collection() {
             return this.table2Collection;
             }
            
             public void setTable2Collection(Set<Table2> table2Collection) {
             this.table2Collection = table2Collection;
             }
            
            }




            Table 2


            @Entity
            public class Table2 implements Serializable {
             @Id
             private BigDecimal eno;
            
             @ManyToOne
             @JoinColumn(name="NO")
             private Table1 no;
            
             private static final long serialVersionUID = 1L;
            
             public Table2() {
             super();
             }
            
             public BigDecimal getEno() {
             return this.eno;
             }
            
             public void setEno(BigDecimal eno) {
             this.eno = eno;
             }
            
             public Table1 getNo() {
             return this.no;
             }
            
             public void setNo(Table1 no) {
             this.no = no;
             }
            
            }


            thank you
            ragrds
            kaviarasu

            • 3. Re: Inserting into primary and secondry table using bean
              trickyvail

              I'm not sure I understand your question correctly, but I think you are asking how to have the primary keys generated for you? Your entity beans do not have the @GeneratedValue annotation on their id fields. This may be what you need to explore.

              • 4. Re: Inserting into primary and secondry table using bean
                kaviarasu

                Hi thanks for ur reply
                it worked atlast