What to call Named Query in main? JPA

Asked

Viewed 78 times

0

Guys, please, could someone show me how to call in the main @Namedquery class of another class. Follow the example:

@Entity
@Table(name="edificio")
@NamedQuery(name="consultaEdificioPorChavePrimaria",query="select e from Edificio e where e.id = :id")

public class Edificio implements Serializable {

//codigo

}

I truly thank you. I embrace you all!

  • Try to provide more details of your problem and if possible an example Minimum, Complete and Verifiable of what you’re trying to accomplish.

  • Hey, buddy, what’s up? I’m learning JPA in a book, and I did a personal example of building registration and he comments on various ways to pull a list from the database and one is the example. Place Annotation in the class you want to list, print database data. The Annotation is @Namedquery. I wonder how I do that in the main,view class, I call that Edificio class the Annotation that indicates to print the information to be compiled.

  • 1

    Gabriel to use @Namedquery you need to be with all JPA already configured. If this is the case you can make the call as follows: em.createNamedQuery("Edificio.consultaEdificioPorChavePrimaria").setParameter("id", "foo").getResultList();

  • @Marlonbahniuk, post your comment as reply :)

1 answer

0

To use the @Namedquery you need to be with all the JPA already configured.

If this is the case you can make the call as follows:

List results = em.createNamedQuery("Edificio.consultaEdificioPorChavePrimaria")
.setParameter("id", 1)
.getResultList();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.