如何在JPA中的db中保存对象后获取id?

时间:2016-07-01 10:21:24

标签: mysql spring jpa spring-boot entitymanager

我在保存claimDetail对象后获取id时面临问题,然后获取该保存对象的id,它将变为0。实际上我想得到保存的对象Id。但它不会来。我没有使用JPA。我已经为调度创建了一个Spring Boot应用程序。

这是我的ClaimDetails.java实体类:

@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()

temp为0;

这是我的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工厂。

@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>

任何人都可以帮我解释一下如何解决这个问题?

由于 Sitansu

2 个答案:

答案 0 :(得分:1)

编写配置类并执行类似的操作。使用JpaRepository

@Configuration
public class ClaimDetailService {


public interface ClaimDetailRepository extends JpaRepository<Claimtrans, String>{
    ClaimDetail findById(String id);
}

    @Autowired
    ClaimDetailRepository claimDetailRepository;

    @Autowired
    public void save(){
        ClaimTrans claimDetail=new ClaimTrans();
        claimDetail.setId(UUID.randomUUID.toString());
         claimDetail.setActive(1);
        claimDetail.setVersion(new Long(1));
        claimDetail.setCreatedBy(new Long(1));
        claimDetail.setCreatedDate(new Date());
        claimDetailRepository.save(claimDetail);

    int temp =claimDetailRepository.findById(claimDetail.getId());

    }

}

答案 1 :(得分:1)

JPA规范不保证提供的实体对象在保存后会更新。要获取已保存的JPA实体,您必须使用save()方法的返回值。例如,您的服务可以通过以下方式更改:

@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的自己的接口。