Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA query returns Object[] instead of desired entity

I use this code to do a query to a sql server database:

List<Continent> continents = new ArrayList<>();
        List<Object[]> tuples = (List<Object[]>) em.createNativeQuery("SELECT * FROM Continents").getResultList();
        for (Object[] tuple : tuples) {

            System.out.println(tuple[0]);

        }
        return continents;

I use this code because I can't get the desired entity type (Continent) straight out of the database.

How should my query look like?

This is the start of my continent class:

@Entity
@Table
public class Continent implements Serializable {
    @Column(name = "Name")
    private StringProperty name;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ContinentID")
    private IntegerProperty continentId;
    @OneToMany(mappedBy="continent")
    private Connection connection;
    List<Country> countries;


    public Continent(String naam) throws SQLException {
        name = new SimpleStringProperty(naam);
        continentId = new SimpleIntegerProperty();
    }

    protected Continent()
    {}

This is my persistence.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="HOGENT1415_11" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>domain.Continent</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:1433;databaseName=HOGENT1415_11"/>
      <property name="javax.persistence.jdbc.user" value="sa"/>
      <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
      <property name="javax.persistence.jdbc.password" value="root"/>
    </properties>
  </persistence-unit>
</persistence>

1 Answers

You should provide resulting class, if you are using native query:

List<Continent> tuples = em.createNativeQuery("SELECT * FROM Continents", Continent.class).getResultList();

or just use jpql query:

List<Continent> tuples = em.createQuery("SELECT c FROM Continent c", Continent.class).getResultList();

In second example result class is optional, but this way you get TypedQuery and can avoid casting. Note that in case of jpql you provide entity name, not table name like in native query.

like image 184
Vladimir Avatar answered Dec 07 '25 23:12

Vladimir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!