Sunday, 1 May 2016

How to delete entity in Spring+Hibernate+JPA


Create the interface for the particular entity as like below

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface TravelDetailRepository extends
JpaRepository<TravelDetail, Long> , JpaSpecificationExecutor<TravelDetail>{
}

Entity is created below , use the JPA tools by right clicking on the project in Eclipse providing the datasource from where the entity has to be created

@Entity
@Table(name="TRAVEL_DETAIL", schema="TRAVEL")
@NamedQuery(name="TravelDetail.findAll", query="SELECT f FROM TravelDetail f")
public class TravelDetail implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private long id;

//get set methods
}

Implementing the DAO as like below

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Repository
public class TravelDetailDaoImpl {
@Autowired
private TravelDetailRepository travelDetailRepository;
public void delete(TravelDetail travelDetail) {
travelDetailRepository.delete(travelDetail);
}
}

No comments:

Post a Comment