본문 바로가기
Dev/[프로젝트] 2022 Spring Boot + JSP

[프로젝트] Spring Boot + JSP를 이용한 함께 부산 여행할 사람을 구하는 웹 사이트-이슈

by javapp 자바앱 2022. 8. 1.
728x90

 

 

MySQL 에 AI 가 걸려있는 테이블 조회시 int 값에 문제 발생 -> Integer 타입으로 해결

java.lang.IllegalArgumentException: Can not set int field com.tour.app.domain.ReviewBoardPK.num to org.hibernate.id.IdentifierGeneratorHelper$2
@IdClass(ReviewBoardPK.class)
public class ReviewBoard 
{
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int num;
	
	@Id
	private int boardId;

 

@Setter @Getter
@NoArgsConstructor 
public class ReviewBoardPK implements Serializable
{
	private int num;
	private int boardId;
}

복합키 중 num에 AI 설정시 발생

 

 

수정

참고

https://shinsunyoung.tistory.com/45

@Getter
@Setter
@Entity
@IdClass(ReviewBoardPK.class)
public class ReviewBoard 
{
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer num;
@Setter @Getter
@NoArgsConstructor 
public class ReviewBoardPK implements Serializable
{
	@Column
	private Integer num;
	private int boardId;

}

 

 

@Service

	public ReviewBoard findById(int num) {
		return tourReviewJpaRepository.findByNum(Integer.valueOf(num));
	}
	ReviewBoard findByNum(Integer num);

 

 

 


 

 

JPA @Query DB 조회 후 DTO 매핑시

No converter found capable of converting from type

https://withseungryu.tistory.com/159

 

[JPA] JPA native query 사용시 dto mapping

보통 조인을 많이 사용하게 될 상황이 생기면 JPA Native query를 사용한다. 이 때, 조인되어지는 테이블들의 column 값을 사용해야하는 상황에는 Entity에 맵핑을 시켜줄 수 없는 문제가 있다. 이럴 때

withseungryu.tistory.com

 

만약 DTO를 그대로 클라이언트에게 주어줘도 상관이 없다면 interface ( with getter) 를 사용

public interface TourReviewDTOInterface 
{
	int getNum();
	String getTitle();
	String getNick_name();
	Date getRegdate();
	int getHitcount();
}

 

public interface TourReviewJpaRepository extends JpaRepository<ReviewBoard, Integer>
{
	@Query(value="SELECT r.num, r.title, m.nick_name, r.regdate, r.hitcount "
			+ "FROM review_board r "
			+ "inner join member m ON r.user_id = m.user_id", nativeQuery=true)
	List<TourReviewDTOInterface> findReviewBoardList();
}

 

 

 


 

 

 

페이지내이션 사용 중 찾기 기능할 때 

한 페이지당 갯수가 초과하는 게시글이 있을 경우 터짐

ex) 한페이지에 8개로 제한 했는데 총 결과가 9개 이면 2개페이지가 필요 -> 에러 발생

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where) from tour_area where area like CONCAT('%','남구','%')' at line 1

 

발생지점

@Query(value="select contents_id, title,contents_name, thumimagefile from tour_area where area like CONCAT('%',:area,'%')" 
    , nativeQuery=true)
Page<TourAreaDTOInterface> findByAreaContaining(String area, Pageable pageable);

 

카디널리티(튜플의 수)가 바뀌면 

직접 카운트 쿼리를 지정하여 페이지 매김을 위한 네이티브 쿼리를 사용할 수 있습니다. 

참고 : https://stackoverflow.com/questions/22345081/spring-data-jpa-query-and-pageable

@Query(value="select contents_id, title,contents_name, thumimagefile from tour_area where area like CONCAT('%',:area,'%')" ,
			countQuery = "SELECT count(*) FROM tour_area WHERE area like CONCAT('%',:area,'%')"
			, nativeQuery=true)
Page<TourAreaDTOInterface> findByAreaContaining(String area, Pageable pageable);

countQuery에 count(*)를 구하는 쿼리문 별도로 작성합니다.

 

 

 


 

 

1:N 연관관계에서 Lazy 로딩시 Userid (키)를 찾을 수 없는 상태

 

Property [userid] not found on type [com.tour.app.domain.ReviewBoard]

 

jsp 에서 ${reviewBoard.member.userid} 여기서 userid를 찾을 수 없었습니다.

 

Member : ReviewBoard = 1 : N

 

Member.java

public class Member {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name="user_id")
	private int userid; //필수, 사용자 ID
    ...

@Column(name="user_id")

 

ReviewBoard.java

@IdClass(ReviewBoardPK.class)
public class ReviewBoard 
{
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer num;
	
	@Id
	private int boardId;
	
	private String title;
	
	// fk
	@JoinColumn(name="user_id")
	@ManyToOne(fetch=FetchType.LAZY)
	private Member member;
	...

@JoinColumn(name="user_id")

 

 

  • @Column(name="user_id") 을 @JoinColumn(name="user_id") 와 일치시킵니다.
  • jsp 에서는 멤버의 변수명으로 호출 ${reviewBoard.member.userid}

 

댓글