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.