简体   繁体   中英

how to use multiple join sql query in java spring boot

i want to use multiple join query in java spring boot i can't found answer so if you know the solution plz comment it

public interface CategoryRepository extends JpaRepository<Category, Integer> {

@Query(value = "SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4 FROM category AS t1 LEFT JOIN category AS t2 ON t2.parent = t1.category_id LEFT JOIN category AS t3 ON t3.parent = t2.category_id LEFT JOIN category AS t4 ON t4.parent = t3.category_id WHERE t1.name = :'ROOT'",nativeQuery = true)
List<Category> findByCategory(String query);
}

this is my CategoryRepository Code

@Data
@Entity
@Table(name="category")
@DynamicInsert
@DynamicUpdate
@NoArgsConstructor
@AllArgsConstructor
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="category_id", columnDefinition = "INT(11)")
private int Category_id;
private String name;
private int parent;

}

this is my Category code

@RestController
@RequestMapping(value = "/category")
@Slf4j
public class CategoryController {
@Autowired CategoryRepository categoryRepository;

@RequestMapping(value = "/all", method = RequestMethod.GET)
@ResponseBody
public String getDomainList(String query) {
    List<Category> all= this.categoryRepository.findByCategory(query);
    return all.toString();
    //log.info(query);
    //return "Test";
}
} 

this is my CategoryController Code

so when i run the Code

2019-10-31 11:10:30.124  WARN 2356 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : 
SQL Error: 0, SQLState: S0022
2019-10-31 11:10:30.124 ERROR 2356 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : 
Column 'category_id' not found

i got the this error message i really crave to run it.. thanks

Check Here- May be solve your issue

Spring JDBCTemplates: execute a query with Join

Try This Method

String query= "SELECT firewall_items.src, firewalls.info 
FROM firewall_items 
JOIN firewalls 
ON firewall_items.firewalls_id = firewalls.id"
List<Item> items = jdbcTemplate.query(
      query, 
      new MapSqlParameterSource(), 
      new FirewallInfoRowMapper()
      );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM