Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 刷新Jpa2后实体id为空_Hibernate_Jpa_Flush - Fatal编程技术网

Hibernate 刷新Jpa2后实体id为空

Hibernate 刷新Jpa2后实体id为空,hibernate,jpa,flush,Hibernate,Jpa,Flush,我在基于spring的Web应用程序中使用Hibernate和Jpa2 当我试图保存多个实体时,我得到了一个乐观锁异常,因为即使调用flush方法,第一个保存的实体Id也是null 事实上,它应该返回insert实体及其id作为hibernate文档状态 实体主键由以下两个值组成: @Column(name = "tagId", nullable = false) @Basic(fetch = FetchType.EAGER) @Id @GeneratedValu

我在基于spring的Web应用程序中使用Hibernate和Jpa2

当我试图保存多个实体时,我得到了一个乐观锁异常,因为即使调用flush方法,第一个保存的实体Id也是null

事实上,它应该返回insert实体及其id作为hibernate文档状态

实体主键由以下两个值组成:

    @Column(name = "tagId", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    Integer tagId;

    @Column(name = "modelId", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    @Id
    Integer modelId;
我认为问题在于标记id字段是自动生成的,而不是作为modelId“注入”的

存储dao方法如下所示:

    @Transactional
        public Tag saveTag(Tag tag) {
            Tag existingTag = tagDAO.findTagByPrimaryKey(tag.getTagId(), tag.getModelId());

            if(existingTag != null){
                if(existingTag != tag){
                    existingTag.setTagId(tag.getTagId());
                    existingTag.setModelId(tag.getModelId());
                    existingTag.setVersion(tag.getVersion());
                    existingTag.setName(tag.getName());
                }
                tag = tagDAO.store(existingTag);
            }else{
                tag = tagDAO.store(tag);
            }
            tagDAO.flush();

            return tag;
          }

建议?

您可以尝试将类似这样的注释添加到id字段中@GenericGenerator(name=“tagIdGenerator”,strategy=GenerationType.SEQUENCE)@GeneratedValue(generator=“tagIdGenerator”)我尝试使用这个@GenericGenerator(name=“tagIdGenerator”,strategy=“increment”)@GeneratedValue(generator=“tagIdGenerator”),但不起作用