在数据库中导入数据时,在自动生成的ID上获取Javax Hibernate ConstraintViolationException

时间:2019-01-05 16:23:19

标签: java hibernate constraints jfs

我已经在数据库中导入了3行(通过类路径中的import.sql),主键为id = 1,id = 2,id = 3。当我尝试通过javax.faces插入新数据时,我连续3次收到ConstraintViolationException,然后在第4次尝试中,使用自动生成的id = 4插入新行。是否有可能避免这3个错误并在表ID中从最后一个ID开始的第一次尝试中插入数据?

这是我的实体声明:

function init(){
    /*creates empty scene object and renderer*/
    scene = new THREE.Scene();
    camera = new THREE.PerspectiveCamera(15, window.innerWidth / window.innerHeight, 0.1, 1000 );
    renderer = new THREE.WebGLRenderer({antialias:true});

    renderer.setClearColor(0x838510);
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.shadowMapEnabled= true;
    renderer.shadowMapSoft = true;

    /*add controls*/
    controls = new THREE.OrbitControls( camera, renderer.domElement );
    controls.addEventListener( 'change', render );

    camera.position.x = -5;
    camera.position.y = 8;
    camera.position.z = 18;    
    camera.lookAt(scene.position);

    /*datGUI controls object*/
    guiControls = new function(){
        this.Bone_0 = 0.0;
        this.Bone_1 = 0.0;
        this.Bone_2 = 0.0;
        this.Bone_3 = 0.0;
        this.Bone_4 = 0.0;
        this.rotationX  = 0.0;
        this.rotationY  = 0.0;
        this.rotationZ  = 0.0;

        this.lightX = 131;
        this.lightY = 107;
        this.lightZ = 180;
        this.intensity = 1.5;       
        this.distance = 373;
        this.angle = 1.6;
        this.exponent = 38;
        this.shadowCameraNear = 34;
        this.shadowCameraFar = 2635;
        this.shadowCameraFov = 68;
        this.shadowCameraVisible=false;
        this.shadowMapWidth=512;
        this.shadowMapHeight=512;
        this.shadowBias=0.00;
        this.shadowDarkness=0.11;

        this.scene = function(){
            console.log(scene);
        };

    }


/*add loader call add model function*/
    loader = new THREE.JSONLoader();
    loader.load( 'modelling/LASTMODEL3.json', addModel );


    /*adds controls to scene*/
    datGUI = new dat.GUI();

    /*edit bones*/
    datGUI.add(guiControls, "scene");
    var cfolder = datGUI.addFolder('Controls');

    cfolder.add(guiControls, 'Bone_0' ,-0.1, 0.1);
    cfolder.add(guiControls, 'Bone_1' ,-0.1, 0.1);
    cfolder.add(guiControls, 'Bone_2' ,-0.1, 0.1);
    cfolder.add(guiControls, 'Bone_3' ,-0.1, 0.1);
    cfolder.add(guiControls, 'Bone_4' ,-0.1, 0.1);
function render() { 
    spotLight.position.x = guiControls.lightX;
    spotLight.position.y = guiControls.lightY;
    spotLight.position.z = guiControls.lightZ;

    scene.traverse(function(child){
        if (child instanceof THREE.SkinnedMesh){  

            child.rotation.x += 0.00;
            child.rotation.y += 0.00;
            child.rotation.z += 0.00;

            child.skeleton.bones[0].rotation.y = guiControls.Bone_0;
            child.skeleton.bones[1].rotation.z = guiControls.Bone_1;
            child.skeleton.bones[2].rotation.x = guiControls.Bone_2;
            child.skeleton.bones[3].rotation.z = guiControls.Bone_3;
            child.skeleton.bones[4].rotation.z = guiControls.Bone_4;
}
        else if  (child instanceof THREE.SkeletonHelper){
            child.update();
        }
    }); 

}

谢谢。

1 个答案:

答案 0 :(得分:0)

谢谢krokodilko。通过添加策略解决了该问题:

@GeneratedValue(strategy=GenerationType.SEQUENCE)

,并在每个表插入import.sql脚本后添加以下行:

SELECT setval([sequencename], max(id)) FROM [tablename];
相关问题