Search code examples
springspring-boothibernatejpaentity

Read Data from MariaDB with SpringBoot JPA


I want to try out a simple Spring Boot Hibernate example.

I have this Database:

create or replace table treasurelog.categories
(
    id   bigint auto_increment
        primary key,
    name varchar(20) null
);

And i want to use JPA Hibernate to get the data from it. The acces is via a CategoriesController and a JPARepository.

As shown above, my Table Categories in the DB, only having the ID as PK and a name.

Then I wrote this controller:

@RestController
public class FindingController {
    private CategoryRepository categoryRepository;

    public FindingController(CategoryRepository categoryRepository) {
        this.categoryRepository = categoryRepository;
    }

    @GetMapping(value = "/categories")
    public List<Category> getCategories() {
        return categoryRepository.findAll();
    }
}

with this Repository

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface CategoryRepository extends JpaRepository<Category, Integer> {

    @Query("select Category as category from Category ")
    public List<Category> findAll();
}

and this Entity:

@Getter
@Setter
@Entity
@Table(name = "categories")
public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

    @Size(max = 20)
    @Column(name = "name", length = 20)
    private String name;

}

But I always get this error while trying a GET:

ava.lang.NullPointerException: Cannot invoke "org.hibernate.metamodel.mapping.DiscriminatorType.getJdbcJavaType()" because "this.discriminatorType" is null
    at org.hibernate.sql.ast.tree.expression.EntityTypeLiteral.createSqlSelection(EntityTypeLiteral.java:132) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.sql.ast.tree.expression.EntityTypeLiteral.createDomainResult(EntityTypeLiteral.java:123) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.lambda$visitSelection$23(BaseSqmToSqlAstConverter.java:2287) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at java.base/java.util.Collections$SingletonList.forEach(Collections.java:4966) ~[na:na]
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitSelection(BaseSqmToSqlAstConverter.java:2282) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitSelectClause(BaseSqmToSqlAstConverter.java:2200) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQuerySpec(BaseSqmToSqlAstConverter.java:2044) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQuerySpec(BaseSqmToSqlAstConverter.java:434) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.tree.select.SqmQuerySpec.accept(SqmQuerySpec.java:125) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.spi.BaseSemanticQueryWalker.visitQueryPart(BaseSemanticQueryWalker.java:221) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQueryPart(BaseSqmToSqlAstConverter.java:1902) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitSelectStatement(BaseSqmToSqlAstConverter.java:1587) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitSelectStatement(BaseSqmToSqlAstConverter.java:434) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.tree.select.SqmSelectStatement.accept(SqmSelectStatement.java:222) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.translate(BaseSqmToSqlAstConverter.java:770) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.buildCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:345) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.withCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:268) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:244) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:518) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:367) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.hibernate.query.Query.getResultList(Query.java:119) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:129) ~[spring-data-jpa-3.1.3.jar:3.1.3]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:92) ~[spring-data-jpa-3.1.3.jar:3.1.3]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:148) ~[spring-data-jpa-3.1.3.jar:3.1.3]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:136) ~[spring-data-jpa-3.1.3.jar:3.1.3]
    at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:136) ~[spring-data-commons-3.1.3.jar:3.1.3]
    at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:120) ~[spring-data-commons-3.1.3.jar:3.1.3]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:164) ~[spring-data-commons-3.1.3.jar:3.1.3]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:143) ~[spring-data-commons-3.1.3.jar:3.1.3]

In the tutorials I saw was nothing written about a DescriminatorType.

I hope somebody can give me some Information.

Thanks.


Solution

  • First of all in repository interface change <Category, Integer> to <Category, Long> because primary key of entity class is Long

    Second one change field declaration in controller from

    private CategoryRepository categoryRepository 
    

    to

    private final CategoryRepository categoryRepository
    

    if you want to have constructor injection

    And last one in entity class Category also declare an default constructor without any arguments and one with all arguments, it is going to help you. A good answer about it you can find here

    P.S. The query in repository should be so:

    @Query("SELECT cat FROM Category cat ")
    public List<Category> findAll();