我有Postgresql + JPA和自动递增键的问题,我只是没有解决。我搜索了一半的一天,但没有任何作用。
在网上我找到了以下解决方案
@Entity(name="FAST_FEATURE")
@SequenceGenerator(name="scanIdGen", initialValue=1000,
sequenceName="scan_seq", allocationSize=1)
public class FastFeature implements Serializable
{
@Id
@SequenceGenerator(name="FAST_FEATURE_id_seq",
sequenceName="FAST_FEATURE_id_seq",
allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator="FAST_FEATURE_id_seq")
@Column(name = "id", updatable=false)
private Long id=null;
....
}
稍后我尝试使用:
将实体持久化到EJB中@Stateless
public class FastRestService {
@PersistenceContext(unitName="FastSearchServerPU")
EntityManager em;
@Context
private UriInfo uriInfo;
...
@POST
@Path("/feature")
public Response createFeature(ServerFeature serverFeature)
{
....
FastFeature fastFeature=new FastFeature();
...
em.persist(fastFeature);
}
如果我运行它,我总是会收到错误消息:
javax.persistence.PersistenceException: Exception [EclipseLink-4002]
(Eclipse Persistence Services - 2.6.1.qualifier):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: FEHLER: doppelter
Schlüsselwert verletzt Unique-Constraint „fast_feature_pkey“
(double key value violates unique-constraint)
Detail: Schlüssel „(id)=(1)“ existiert bereits.
Error Code: 0
Call: INSERT INTO FAST_FEATURE (id, DOES_EXIST, HARD_CONSTRAINT, NAME,
FAST_FEATURE_SET_ID, FAST_FEATURE_TYPE) VALUES (?, ?, ?, ?, ?, ?)
bind => [6 parameters bound]
Query: InsertObjectQuery(ch.hslu.mse.fasttrack.model.FastFeature[ id=1 ])
at
org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl$1.handleException(EntityManagerSetupImpl.java:745)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.handleException(AbstractSynchronizationListener.java:275)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.beforeCompletion(AbstractSynchronizationListener.java:170)
at org.eclipse.persistence.transaction.JTASynchronizationListener.beforeCompletion(JTASynchronizationListener.java:68)
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:452)
at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:854)
at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:721)
at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:505)
感谢任何评论。
添