如何在JPA中的db中保存对象后获取id? [英] How to get id after saved object in db in JPA?

查看:58
本文介绍了如何在JPA中的db中保存对象后获取id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在保存 claimDetail 对象后获取 id 时遇到问题,然后获取该保存对象的 id 它即将到来 0 .其实我想得到那个保存的对象 Id .但它不会来.我没有使用 JPA.我已经创建了一个用于调度的 Spring Boot 应用程序.

I facing issue when i am getting id after saving the claimDetail object then to get the id of that saved object it is coming 0 . Actually I want get that saved object Id .But it not coming. I did not work with JPA. I have created a Spring Boot application for scheduling .

这是我的 ClaimDetails.java 实体类:

Here is my ClaimDetails.java entity class:

@Entity
@Table(name = "claimtrans")
public class ClaimTrans {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
}

    claimDetail.setActive(1);
    claimDetail.setVersion(new Long(1));
    claimDetail.setCreatedBy(new Long(1));
    claimDetail.setCreatedDate(new Date());
    claimDetailService.saveClaimDetail(claimDetail);

int temp =claimDetail.getID()

温度为 0;

这是我的 JpaRepositoryFactory.java:

Here is my JpaRepositoryFactory.java:

@Service
public class ClaimDetailService {

    private JpaRepositoryFactory jpaRepositoryFactory;

    @Autowired
    public ClaimDetailService(JpaRepositoryFactory jpaRepositoryFactory) {
        this.jpaRepositoryFactory = jpaRepositoryFactory;
    }

    @Transactional
    public void saveClaimDetail(ClaimDetail claimDetail) {
        JpaRepository<ClaimDetail, Long> mailAuditLogLongJpaRepository = jpaRepositoryFactory.getRepository(ClaimDetail.class);
        mailAuditLogLongJpaRepository.save(claimDetail);
    }

    public List<ClaimDetail> getAllClaimDetail() {
        JpaRepository<ClaimDetail, Long> mailAuditLogLongJpaRepository = jpaRepositoryFactory.getRepository(ClaimDetail.class);
        return mailAuditLogLongJpaRepository.findAll();
    }
}

这是我的 JPA 工厂.

Here is my JPA Factory.

@Component
public class JpaRepositoryFactory {

    @PersistenceContext
    private EntityManager entityManager;

    public <T> T getRepository(Class clazz) {
        notNull(clazz);
        notNull(entityManager);
        T crudRepository = (T) new SimpleJpaRepository(clazz, entityManager);
        return crudRepository;
    }
}   

pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>org.sam.application.Application</start-class>
        <java.version>1.6</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.10</version>
        </dependency>

谁能帮我解决这个问题?

Anyone can help me please how to fix this issue ?

谢谢斯坦苏

推荐答案

JPA 规范不保证提供的实体对象在保存后会更新.要获得保存的 JPA 实体,您必须使用 save() 方法的返回值.例如,您的服务可以这样更改:

The JPA spec doesn't guarantee that the provided entity object will be updated after saving it. To get the saved JPA entity you have to use the return value of the save() method. For example, your service could be changed this way:

@Service
public class ClaimDetailService {
    ...
    @Transactional
    public ClaimDetail saveClaimDetail(ClaimDetail claimDetail) {
        JpaRepository<ClaimDetail, Long> mailAuditLogLongJpaRepository = jpaRepositoryFactory.getRepository(ClaimDetail.class);
        return mailAuditLogLongJpaRepository.save(claimDetail);
    }
    ...
}

您的示例代码将是:

claimDetail.setActive(1);
claimDetail.setVersion(new Long(1));
claimDetail.setCreatedBy(new Long(1));
claimDetail.setCreatedDate(new Date());
ClaimDetail savedClaimDetail = claimDetailService.saveClaimDetail(claimDetail);
int temp = savedClaimDetail.getID()

此外,虽然与您的问题没有直接关系,但您不需要按照您的方式创建 Spring Data 存储库.只需创建您自己的接口扩展 JPARepository.

Also, although not directly related to your problem, you don't need to create the Spring Data repositories the way you have done it. Just create your own interface extending JPARepository.

这篇关于如何在JPA中的db中保存对象后获取id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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