0

In my spring boot app, I am using spring data jpa. In the entity, I need to pick schema name from config and the schema would change and needs to be configurable. I tried the following but it doesn't work

@Entity
@Table(schema="${schema.name}", name="MyTable")

schema.name is define in application.properties file

I get "could not extract resultset" error

Is there a way to accomplish this ?

===============EDIT===============

@Entity
@Table(name="MyTable")
public class MyData {

    @Id
    @Column(name="MyID")
    @JsonProperty("MyID")
    private String MyID;

    @Column(name="number")
    @JsonProperty("number")
    private String number;

    @Column(name="value")
    @JsonProperty("value")
    private String value;

    ......getter and setters go here.....
}
Hary
  • 1,127
  • 4
  • 24
  • 51

2 Answers2

1

Use

spring.jpa.properties.hibernate.default_schema=schema

You can see this similar question here.

Audrey Carval
  • 168
  • 2
  • 7
0

Please set the schema and other DB related config in application.properties file.
Its not good practice to keep schema name in Entity.

like below code:

spring.datasource.url=jdbc:mysql://192.168.97.39:3309/schema-name?autoReconnect=true&autoReconnectForPool=true&characterEncoding=UTF-8
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

spring.datasource.username=userName
spring.datasource.password=Password

Here Schema name configured in spring.datasource.url property

TheSprinter
  • 1,523
  • 17
  • 30
  • I removed schema name from @Table and moved it to spring.datasource.url. Got following error now. Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: Table [MyTable] contains logical column name [txnid] referring to multiple physical column names: [txnid], [txn_id]. – Hary May 31 '18 at 17:30
  • Remive all the schema from all the entity – TheSprinter Jun 03 '18 at 13:46
  • I have one entity only and removed schema named from that. The error explained above is a result of that. – Hary Jun 04 '18 at 08:10
  • as per the error message"DuplicateMappingException", the exception coming bacause of wrong mapping. check '@ManytoOne', @'OneToMany' 0r '@ManyToMany'anontations or share the entity file – TheSprinter Jun 04 '18 at 08:32
  • There aren't any associations . Just using one single table – Hary Jun 04 '18 at 15:26
  • can you please share that file – TheSprinter Jun 05 '18 at 10:56
  • entity looks ok, i need to see some more thing, like Db table(MyTable), and repository code, properties file. if possible please share all the files with table – TheSprinter Jun 06 '18 at 06:51