简体   繁体   中英

Spring JPA data repository not acting like it should

I am new to Spring and I am trying to create an Object and add it to my database and then get the value from it. As far as I understand I should not add any extra lines and findAll should return me a proper looking String as a result.
But the result I get looks like this:

[model.Orders@4a163575, model.Orders@7ecec90d]

What I also understood is that I should not add get/set methods to Spring as they should be automatically generated, but when I try to cast the model.Orders@4a163575 into an Object and do the get method It tells me that there is no get method.

So here is my Object :

@Data
@Entity
public class Orders {

    public Orders(String orderName) {
        this.orderName = orderName;
    }

    public Orders() {
    }

    @Id
    @GeneratedValue
    private Long id;

    private String orderName;

}

Then the findAll method:

@Repository
public class OrderDao {

        public List<Orders> findAll(){
            return em.createQuery("select p from Orders p", Orders.class).getResultList();
        }
}

And where I launch it all:

public static void main(String[] args) {

        ConfigurableApplicationContext ctx =
              new AnnotationConfigApplicationContext(DbConfig.class);

        OrderDao dao = ctx.getBean(OrderDao.class);

        dao.save(new Orders("order1"));
        dao.save(new Orders("order2"));

        System.out.println(dao.findAll());
}

From what I have I think that the @Data annotation is not working properly since there is no toString nor getters/setter .

I import the @Data annotation with this line : import lombok.Data; .

What am I doing wrong here.

You need to install lombok plugin for that it will be possible generating them

You can refer to this article how to install lombok in IntellijIdea:

Lombok annotations do not compile under Intellij idea

Please also add enable annotation processing

Well it seems I had forgotten to enable annotation processing . Picture to show where and how I did it

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