6 Replies Latest reply on Apr 30, 2007 4:53 PM by Clinton Parham

    Example: OneToMany & Composite Key

    Clinton Parham Newbie

      Hello: i'm trying to put together a working Seam example showing a OneToMany relationship using a composite primary key. Something like Order & LineItems from the Java EE 5 tutorial. My attempts have been unsuccessful so I'm hoping someone can tell me where I can find a Seam deployable example that actually works.

      I have tried the Java EE 5 tutorial code (\javaeetutorial5\examples\ejb\order) and it fails with the following (against an Oracle database):

      20:56:11,991 INFO [STDOUT] Hibernate: insert into EJB_ORDER_ORDER (status, lastUpdate, discount, shipmentInfo, orderId) values (?, ?, ?, ?, ?)
      20:56:12,084 INFO [STDOUT] Hibernate: insert into EJB_ORDER_LINEITEM (ORDERID, VENDORPARTNUMBER, quantity, orderId, itemId) values (?, ?, ?, ?, ?)
      20:56:12,240 WARN [JDBCExceptionReporter] SQL Error: 957, SQLState: 42000
      20:56:12,240 ERROR [JDBCExceptionReporter] ORA-00957: duplicate column name
      
      20:56:12,240 WARN [JDBCExceptionReporter] SQL Error: 957, SQLState: 42000
      20:56:12,240 ERROR [JDBCExceptionReporter] ORA-00957: duplicate column name
      
      20:56:12,240 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
      org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
       at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
       at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
       at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
       at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)


      Notice that orderId is listed twice in the second insert statement above?? The code is available here: http://java.sun.com/javaee/5/docs/tutorial/information/download.html

      Please help! I really want to use Seam.


        • 1. Re: Example: OneToMany & Composite Key
          Pete Muir Master

          All of the examples in the seam distribtution work - or do you want a specific example?

          Show the entity that is producing the error - perhaps someone can help you :)

          • 2. Re: Example: OneToMany & Composite Key
            Clinton Parham Newbie

             

            "petemuir" wrote:
            All of the examples in the seam distribtution work - or do you want a specific example?

            Show the entity that is producing the error - perhaps someone can help you :)


            Yes, I'm looking for a specific example: one that shows use of a Composite Primary key - as the order example in the Java EE tutorial does.

            Here's some code that produces the above duplicate column insert errors (seam 1.2.1 GA). In this instance, the recordId column is duplicated:

            package com.mydomain.selfEnroll;
            
            import java.io.Serializable;
            import java.util.Calendar;
            import java.util.Collection;
            import java.util.Date;
            
            import javax.persistence.CascadeType;
            import javax.persistence.Entity;
            import javax.persistence.GeneratedValue;
            import javax.persistence.GenerationType;
            import javax.persistence.Id;
            import javax.persistence.OneToMany;
            import javax.persistence.SequenceGenerator;
            import javax.persistence.Table;
            import javax.persistence.Temporal;
            import javax.persistence.TemporalType;
            
            import org.hibernate.validator.NotNull;
            import org.jboss.seam.ScopeType;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.annotations.Scope;
            
            @Entity
            @Scope(ScopeType.CONVERSATION)
            @Name("record")
            @Table(name = "RECORD")
            @SequenceGenerator(name = "recordGen", allocationSize = 1, sequenceName = "RECORD_SEQ")
            public class Record implements Serializable {
             private Long recordId;
            
             private Date eventDate;
            
             private Byte memberDiabetes;
            
             private Byte familyDiabetes;
            
             private Byte weight;
            
             private Byte waist;
            
             private Collection<RecordData> recordData;
            
             public Record() {
             eventDate = Calendar.getInstance().getTime();
             }
            
             @NotNull
             @Temporal(TemporalType.DATE)
             public Date getEventDate() {
             return eventDate;
             }
            
             public void setEventDate(Date eventDate) {
             this.eventDate = eventDate;
             }
            
             public Byte getFamilyDiabetes() {
             return familyDiabetes;
             }
            
             public void setFamilyDiabetes(Byte familyDiabetes) {
             this.familyDiabetes = familyDiabetes;
             }
            
             public Byte getMemberDiabetes() {
             return memberDiabetes;
             }
            
             public void setMemberDiabetes(Byte memberDiabetes) {
             this.memberDiabetes = memberDiabetes;
             }
            
             @Id
             @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "recordGen")
             public Long getRecordId() {
             return recordId;
             }
            
             public void setRecordId(Long recordId) {
             this.recordId = recordId;
             }
            
             public Byte getWaist() {
             return waist;
             }
            
             public void setWaist(Byte waist) {
             this.waist = waist;
             }
            
             public Byte getWeight() {
             return weight;
             }
            
             public void setWeight(Byte weight) {
             this.weight = weight;
             }
            
             @OneToMany(cascade = CascadeType.ALL, mappedBy = "record")
             public Collection<RecordData> getRecordData() {
             return recordData;
             }
            
             public void setRecordData(Collection<RecordData> recordData) {
             this.recordData = recordData;
             }
            }
            
            package com.mydomain.selfEnroll;
            
            import java.io.Serializable;
            
            import javax.persistence.Column;
            import javax.persistence.Entity;
            import javax.persistence.Id;
            import javax.persistence.IdClass;
            import javax.persistence.JoinColumn;
            import javax.persistence.ManyToOne;
            import javax.persistence.SequenceGenerator;
            import javax.persistence.Table;
            
            import org.jboss.seam.ScopeType;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.annotations.Scope;
            
            @IdClass(RecordDataKey.class)
            @Entity
            @Scope(ScopeType.CONVERSATION)
            @Name("recordData")
            @Table(name = "RECORD_DATA")
            @SequenceGenerator(name = "recordGen", allocationSize = 1, sequenceName = "RECORD_SEQ")
            public class RecordData implements Serializable {
             private Long recordId;
            
             private Long biomarkerId;
            
             private Double value;
            
             private Record record;
            
             @ManyToOne
             @JoinColumn(name = "RECORDID")
             public Record getRecord() {
             return record;
             }
            
             public void setRecord(Record record) {
             this.record = record;
             }
            
             public RecordData() {
             }
            
             @Id
             @Column(name = "RECORDID", nullable = false, insertable = false, updatable = false)
             public Long getRecordId() {
             return recordId;
             }
            
             public void setRecordId(Long recordId) {
             this.recordId = recordId;
             }
            
             @Id
             public Long getBiomarkerId() {
             return biomarkerId;
             }
            
             public void setBiomarkerId(Long biomarkerId) {
             this.biomarkerId = biomarkerId;
             }
            
             public Double getValue() {
             return value;
             }
            
             public void setValue(Double value) {
             this.value = value;
             }
            
            }
            
            package com.mydomain.selfEnroll;
            
            import java.io.Serializable;
            
            public final class RecordDataKey implements Serializable {
            
             public Long recordId;
            
             public Long biomarkerId;
            
             public RecordDataKey() {
             }
            
             public RecordDataKey(Long recordPk, Long biomarkerPk) {
             this.recordId = recordPk;
             this.biomarkerId = biomarkerPk;
             }
            
             public boolean equals(Object otherOb) {
             if (this == otherOb) {
             return true;
             }
             if (!(otherOb instanceof RecordDataKey)) {
             return false;
             }
             RecordDataKey other = (RecordDataKey) otherOb;
             return ((recordId == null ? other.recordId == null : recordId
             .equals(other.recordId)) && (biomarkerId == other.biomarkerId));
             }
            
             public int hashCode() {
             return ((recordId == null ? 0 : recordId.hashCode()) ^ ((int) biomarkerId
             .longValue() >>> 32));
             }
            
             public String toString() {
             return "" + recordId + "-" + biomarkerId;
             }
            
             public Long getBiomarkerId() {
             return biomarkerId;
             }
            
             public void setBiomarkerId(Long biomarkerId) {
             this.biomarkerId = biomarkerId;
             }
            
             public Long getRecordId() {
             return recordId;
             }
            
             public void setRecordId(Long recordId) {
             this.recordId = recordId;
             }
            }
            


            ...
            // code that invokes above entities to produce the problem
            Collection<RecordData> biomarkers = new ArrayList<RecordData>(1);
            RecordData biomarker = new RecordData();
            biomarker.setBiomarkerId(99L);
            biomarker.setValue(myRecord.getMemberDiabetes().doubleValue());
            biomarkers.add(biomarker);
            myRecord.setRecordData(biomarkers);
            
            em.persist(myRecord);
            ...
            


            Thank you.


            • 3. Re: Example: OneToMany & Composite Key
              Clinton Parham Newbie

              Actually, use this code instead of the above to invoke the entities. I just changed one line to make it standalone.

              ...
              // code that invokes above entities to produce the problem
              Collection<RecordData> biomarkers = new ArrayList<RecordData>(1);
              RecordData biomarker = new RecordData();
              biomarker.setBiomarkerId(99L);
              biomarker.setValue(4.4);
              biomarkers.add(biomarker);
              myRecord.setRecordData(biomarkers);
              
              em.persist(myRecord);
              ...
              


              • 4. Re: Example: OneToMany & Composite Key
                Clinton Parham Newbie

                Can anyone offer some insight? Please!

                • 5. Re: Example: OneToMany & Composite Key
                  Pete Muir Master

                  There are lots of examples of mappings in the hibernate annotations test's - this is really a hibernate question - you should take this to hibernate forums.

                  • 6. Re: Example: OneToMany & Composite Key
                    Clinton Parham Newbie

                     

                    "petemuir" wrote:
                    There are lots of examples of mappings in the hibernate annotations test's - this is really a hibernate question - you should take this to hibernate forums.


                    I considered that before posting this question here but if you go to the Hibernate User forum, you'll see that they ask that people post to the Seam forum when questions involve using Hibernate in Seam:
                    http://hibernate.org/20.html#A2