JPA(CRUD) - snippets

anonymous·2022년 6월 30일
0

Service

// INSERT
Object objectData = ObjectDTO.toEntity(objectDTO);
for(ObjectDetail one : objectData.getObject()){
	objectRepository.save(one);
}

// Custom Query
@Query(value = ""
	+"	SELECT count(A.newCnt)                                                                                     "
	+"	FROM (SELECT table.makeDate AS newCnt                                                                 "
	+"		  FROM data.Table table                                                                             "
	+"		  WHERE table.makeDate  >= DATE_ADD(NOW(), INTERVAL -1 HOUR)                                      "
	+"		  ) A                                                                                                  "
	+"		   " , nativeQuery = true)                                                                           
public Long countObject();

Entity

@Entity
@Getter
@Builder
@DynamicUpdate
@NoArgsConstructor // 빈 생성자 필요
@AllArgsConstructor
@Table(name = "TABLE_NAME")
public class Entity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "SEQ")
    private int id;

    @Column(name = "REG_DT")
    private LocalDateTime regDate;

    @Column(name = "USER_ID")
    private String userId;

    //insert, update
    public void setUserId(String userId) {this.userId = userId;}
}

Repository

public interface ObjectRepository extends JpaRepository<Entity, Long>, ObjecRepositoryCustom {
	// SELECT
    public Page<Object> findByCategory(Pageable pageable, Category category);
}

Repository-QueryDSL Interface

public interface EntityRepositoryCustom {
    public void updateEntity(EntityDTO entityDTO);
    public int deleteById(Long id);
}

QueryDSL

public class ObjectRepositoryImpl extends QuerydslRepositorySupport implements ObjectRepositoryCustom {
    public ObjectRepositoryImpl() { super(Entity.class); }
	
    // UPDATE
    @Override
    @Transactional
    public void updateEntity(EntityDTO entityDTO) {
        update(entity)
			.where(
					entity.id.eq(entityDTO.getId())
			)
			.set(entity.title, entityDTO.getTitle())
			.execute();
    }
    
    // DELETE
    @Override
    public int deleteById(Long id) {
      Long return =  delete(category).where(category.id.eq(id)).execute();
      return return.intValue();
    }
}
profile
기술블로거입니다

0개의 댓글