Reputation: 331
I am trying to build app on Spring boot, with postgres. I am using docker-compose. I got an error when I tried to access data Here is exception
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: table is not mapped [SELECT t FROM table t WHERE t.code = :code]
app_1 | at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:138)
app_1 | at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
app_1 | at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
app_1 | at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:718)
app_1 | at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:809)
app_1 | at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)
Method TableRepository
public Table getDataByCode(String tc) {
try {
TypedQuery<Table> q = em.createQuery("SELECT t FROM table t WHERE t.code = :code", Table.class);
q.setParameter("code", tc);
return q.getSingleResult();
} catch (Exception exception) {
exception.printStackTrace();
return null;
}
}
function is called by Table t = tableRepository.getDataByCode("CCC");
My Entity Table
package hello.world.entity;
import javax.persistence.*;
@Entity
@Table(name="table")
public class Table {
@Id
@Column(name="code", length=3, nullable=false)
private String code;
@Column(name="name", length=256, nullable=false)
private String name;
public Table() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Upvotes: 0
Views: 2586
Reputation: 1347
You can do the SELECT
from a table with a specific field implementing the JpaRepository
interface and using the findByField(...)
definition, where Field
is the name of the actual field you want to add in the WHERE
clause.
If you want to do select by code, you can do the following:
@Repository
public interface TableRepository extends JpaRepository<Table, String> {
List<Table> findByCode(String code);
}
In this way you don't have to add the implementation of the code, Jpa will take care of it for you.
Then you can call it simply like this:
Table t = tableRepository.findByCode("CCC");
Remember that you should have in your application.properties file some properties like these to connect to your database:
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres_demo
spring.datasource.username= rajeevkumarsingh
spring.datasource.password=
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
As you can find in this article for example.
Let me know if you need more information.
Upvotes: 1