Reputation: 3329
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 .
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()
temp is 0;
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();
}
}
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 ?
Thanks Sitansu
Upvotes: 5
Views: 17174
Reputation: 1246
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);
}
...
}
And your sample code would be:
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()
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.
Upvotes: 5
Reputation: 649
Write a configuration class and do something like this. Use 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());
}
}
Upvotes: 1