Hi folks,
Currently I'm trying to store one element/object in two collections.
According to the generated database schema this should work just fine.
However, if I don't reset the id of my object to null, it does not work.
There must be a way to 'store' exactly the same object within two collections ...
Ideas?
@Entity
public class Candle implements Comparable<Candle> {
private Long id;
public Candle() {
}
@Id @GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
@MappedSuperclass
public abstract class Series<T> extends Vector<T> {
private Long id;
protected Series() {
}
@Id @GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToMany(cascade=CascadeType.ALL)
@IndexColumn(name="id")
protected T[] getData() {
return Arrays.asArray(this, getChildClass());
}
protected void setData(T[] data) {
setData(Arrays.asList(data));
}
}
@Entity
public class CandleSeries extends Series<Candle> {
...
}
public static void main(String... args) {
// clear
candleDao.deleteAll();
// create objects
Candle candle = new Candle();
CandleSeries series1 = new CandleSeries(candle);
CandleSeries series2 = new CandleSeries(candle);
// save
candleDao.update(series1);
candleDao.update(series2);
}
Caused by: java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL070825051501951' defined on 'CANDLESERIES_CANDLE'.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeUpdate(Unknown Source)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1146)
... 28 more
Caused by: java.sql.SQLException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL070825051501951' defined on 'CANDLESERIES_CANDLE'.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 40 more
|