inserting value in primary and secondry table using onetoone
kaviarasu Aug 2, 2007 8:55 AMhi im using two tables Table1 and Table2
Table1 fields
no-primary coloumn
Table2 fields
no-foreign key to table1
eno-primary key of this table
i want tto generate the value automatically in table1 so it want to update the same value in Table2 no and eno without using trigger . The value want to generate and insert after the submit button pressed in JSF page
import java.io.Serializable; import java.math.BigDecimal; import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; import org.jboss.seam.annotations.Name; @Entity public class Table1 implements Serializable { private static final long serialVersionUID = 1L; @Id private BigDecimal no; private Table2 eno; public Table1() { super(); } @GeneratedValue public BigDecimal getNo() { return this.no; } public void setNo(BigDecimal no) { this.no = no; } @OneToOne(cascade = CascadeType.ALL) @PrimaryKeyJoinColumn public Table2 getEno() { return eno; } public void setEno(Table2 eno) { this.eno = eno; } }
import java.io.Serializable; import java.math.BigDecimal; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToOne; @Entity public class Table2 implements Serializable { @Id private BigDecimal eno; private BigDecimal no; private static final long serialVersionUID = 1L; public Table2() { super(); } public BigDecimal getNo() { return this.no; } public void setNo( BigDecimal no) { this.no = no; } public BigDecimal getEno() { return this.eno; } public void setEno(BigDecimal eno) { this.eno = eno; } }
import java.util.List; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import kavi.Table1; import model.TblUserReviews; import model.Write; import org.hibernate.validator.Valid; import org.jboss.seam.annotations.IfInvalid; import org.jboss.seam.annotations.In; import org.jboss.seam.annotations.Name; import org.jboss.seam.annotations.Outcome; @Stateless @Name("register") public class RegisterAction implements Register { @In @Valid private Table1 user1; @PersistenceContext private EntityManager em; @IfInvalid(outcome = Outcome.REDISPLAY) public String register() { em.persist(user1); return "success"; } }
<table border="0" style=" width : 771px;"> <tr> <td>Rate the product</td> <td><rich:inputNumberSlider value="#{kavi.no}"/> </td> <td> </td> </tr> <tr style=" height : 6px;"> <td>Review Title</td> <td><h:inputTextarea value="#{kavi.eno}" style=" width : 445px; height : 34px;"></h:inputTextarea> </td> </tr> </table> <font color="red"><h:messages /></font> <h:commandButton styleClass="formButton" type="submit" value="Register" action="#{register.register}" />
Thank ypu
Regards
kavi