[jboss-user] [JBoss Seam] - Re: Example: OneToMany & Composite Key

umk do-not-reply at jboss.com
Sat Apr 28 10:08:35 EDT 2007


"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.


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4041641#4041641

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4041641



More information about the jboss-user mailing list