0

when i run the code i got the object

[[Ljava.lang.Object;@8f17f7c, [Ljava.lang.Object;@6c0a4f24, 
 [Ljava.lang.Object;@be4886c, [Ljava.lang.Object;@1760591d, 
 [Ljava.lang.Object;@14e9ce12, [Ljava.lang.Object;@2aa4c0c4, 
 [Ljava.lang.Object;@5ac9a14]

so.. i want to get the String result which in the below plz teach me the way

[Dataset_info(Ds_id=1111, ds_code=a, ds_name=e, ds_category=g, ds_stru=q, insert_ddtt=null, update_ddtt=null), Dataset_info(Ds_id=11111, ds_code=z, ds_name=eww, ds_category=g, ds_stru=q, insert_ddtt=null, update_ddtt=null)]

@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 Integer Category_id;
    @Column(name = "name", columnDefinition = "VARCHAR(20)")
    private String name;
    @Column(name = "parent", columnDefinition = "int(11)")
    private Integer 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 getCategoryList() {
        List < Object[] > all =
            this.categoryRepository.findByCategory();
        return all.toString();
        //log.info(query);
        //return "Test";
    }
}

this is my CategoryController code

import java.util.List;


@Repository
public interface CategoryRepository extends
JpaRepository < Category, Integer > {
    public static final String FIND_PROJECTS = "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'
    ";

    @Query(value = FIND_PROJECTS, nativeQuery = true)
    public List < Object[] > findByCategory();

}

this is my CategoryRepository Code

private void mysql2() {

    this.categoryRepository.findByCategory();
}

this is my application Code for running

so plz teach me i crave to know the way thank you

1
  • You have to implement and overide toString() method on you entity class (Category) Commented Oct 31, 2019 at 9:09

2 Answers 2

1

You can use Projection to contain these values :

public interface CategoryProjection {
   public String getLev1();
   public String getLev2();
   public String getLev3();
   public String getLev4();
}

Then use the interface Projection with Repository :

//...
@Query(value = FIND_PROJECTS, nativeQuery = true)
public List<CategoryProjection> findByCategory();

How to access values in Projections

Because It's a interface, only have getter method.

  • Using Loop (foreach loop, fori loop, ...)

ex :

List<CategoryProjection> list = categoryRepository.findByCategory();

list.forEach(c -> {
      System.out.println(c.getLev1() + " - " + c.getLev2());
});
// loop i
for (int i = 0; i < list.size(); i++) {
   System.out.println(list.get(i).getLev1() + " - " + list.get(i).getLev2());         
}
  • Using index

ex : to get Object Category in index 0

String lev1 = list.get(0).getLev1();
Sign up to request clarification or add additional context in comments.

6 Comments

i change the code and it worked but.. i got the result [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap@5ba6132b, org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap@28264619, org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap@6ab78994, org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap@50dd821f] how to.. get the contents not Object.. could you tell me
Hi @hognkun, If you still here, please have a look again at my answer. I already included how to access the value for projection too. thanks for asking me :) .
thanks a lot it worked for me but now the result printed in console not on the web page so is there any way to show the spring boot webpage?
i changed all the things i appreciated it thankyou so much
@hognkun yes, but can you make my answer as the correct answer plz
|
0

UPD: In your case, I think you can change the method return type to List, HttpMessageConverter would convert the result as JSON String to client. Hope it help.

@ResponseBody
public List getCategoryList() {
    List<Object[]> all = this.categoryRepository.findByCategory();
    return all;
}

Override toString() method in your POJO object. For example:

public class Category {
    private Integer Category_id;
    private String name;
    private Integer parent;
    //omitted getter/setter

    @Override
    public String toString() {
        return "Category{" +
                "Category_id=" + Category_id +
                ", name='" + name + '\'' +
                ", parent=" + parent +
                '}';
    }
}

1 Comment

It doesn't make sense because his return type are not of POJO class !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.