如何结合手动插入和JPA Id生成? [英] How to combine manual insert and JPA Id generation?

查看:1213
本文介绍了如何结合手动插入和JPA Id生成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用arquillian进行容器内测试。我通过向部署添加 import.sql 来预先填充数据库。在测试期间,我想创建更多的实体。



不幸的是,这个失败的原因是 PersistenceException


javax.persistence.PersistenceException:
org.hibernate.exception.ConstraintViolationException:唯一索引或
主键违例:PRIMARY_KEY_BE ON
PUBLIC.KVS_MIPO_DOWNLOAD(ID)

如果我不预先填充数据库,或者不保留新的实体,则一切运行顺利。



id是唯一唯一的字段,所以我强烈怀疑它必须是使用序列生成id。

  @Entity 
@Table(name =KVS_MIPO_DOWNLOAD)
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name =类型,discriminatorType = DiscriminatorType.STRING)
public abstract class DownloadResource实现Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Integer id ;

该实体是另一个具体实体的超类,它不添加任何唯一属性。



可以做些什么来适应这两种可能性 - 手动插入和使用生成的ID?

谢谢



我在JBoss 7.1.1中通过Hibernate 4.0.1使用JPA 2。数据库是Sybase ASE 15。



编辑:目前为止我发现的一种解决方法是将手动添加的实体的ID设置得足够高避免碰撞。但是这对于生产来说还不够好 - 太多的员工可以写入数据库,并且可能会尝试手动添加内容。

解决方案

为您的手动ID使用负数值。 Hibernate不应该生成负面的。



或者,使用你自己的id生成器来跳过一个特定的范围(或跳过可被7整除的数字;一些这样的模式)。



示例ID生成器:

  import org.hibernate.HibernateException; 
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;

public class MyGenerator实现IdentifierGenerator {

public Serializable generate(SessionImplementor session,Object object)
throws HibernateException {
return 1; // TODO:你的方案创建一个Integer;


$ / code>

使用如下注释:

  @Id 
@GeneratedValue(strategy = GenerationType.AUTO,generator =myid)
@GenericGenerator(name = myid,strategy =com.xyzMyDGenerator)
public int getId(){
return _id;
}


I am running in-container tests with arquillian. I am prepopulating the database by adding an import.sql to the deployment. During the test I would like to create some more entities.

Unfortunately, this fails with a PersistenceException:

javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Unique index or primary key violation: "PRIMARY_KEY_BE ON PUBLIC.KVS_MIPO_DOWNLOAD(ID)"

If I do not prepopulate the DB, or do not persist new entities, everything runs smoothly.

The id is the only unique field, so I strongly suspect that it must be the id generation using a sequence.

@Entity
@Table(name = "KVS_MIPO_DOWNLOAD")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class DownloadResource implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Integer id;

This entity is the superclass of another concrete entity, which does not add any unique attributes.

What can be done to be able to accomodate both possibilitiees - inserting manually and using a generated id?

Thank you

I am working with JPA 2 over Hibernate 4.0.1 in JBoss 7.1.1. The database is Sybase ASE 15.

EDIT: one workaround I have found so far is to set the Ids of the manually added entities high enough to avoid collisions. But this is not good enough for production - too many employees have write access to the db and may be tempted to add stuff manually. I would prefer the application to be robust enough not to die and explode in this case.

解决方案

Use negative values for your manual ids. Hibernate shouldn't generate negative ones.

Optionally, use your own id generator that skips a particular range (or skips say numbers divisible by 7; some such scheme).

Sample ID generator:

import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.IdentifierGenerator;

public class MyGenerator implements IdentifierGenerator {

    public Serializable generate(SessionImplementor session, Object object)
            throws HibernateException {
        return 1; // TODO: Your scheme to create an Integer;
    }
}

To use this annotate as follows:

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "myid")
@GenericGenerator(name = "myid", strategy = "com.x.y.z.MyDGenerator")
public int getId() {
    return _id;
}

这篇关于如何结合手动插入和JPA Id生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆