【问题标题】:Exception Could not resolve placeholder异常无法解析占位符
【发布时间】:2021-06-23 02:00:40
【问题描述】:

总的来说,我会尽量描述问题。一个工作项目一度停止工作。修改并尝试了其他帖子的许多选项,直到没有任何帮助。就是这样出来的:

Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Tabelle "USER" nicht gefunden
Table "USER" not found; SQL statement:
insert into user (ID, AKTIV, PASSWORD, START, USERROLE, USERNAME, VERSION) values (null, ?, ?, ?, ?, ?, ?) [42102-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:453)
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
    at org.h2.message.DbException.get(DbException.java:205)
    at org.h2.message.DbException.get(DbException.java:181)
    at org.h2.command.Parser.readTableOrView(Parser.java:7628)
    at org.h2.command.Parser.readTableOrView(Parser.java:7599)
    at org.h2.command.Parser.parseInsert(Parser.java:1747)
    at org.h2.command.Parser.parsePrepared(Parser.java:954)
    at org.h2.command.Parser.parse(Parser.java:843)
    at org.h2.command.Parser.parse(Parser.java:815)
    at org.h2.command.Parser.prepareCommand(Parser.java:738)
    at org.h2.engine.Session.prepareLocal(Session.java:657)
    at org.h2.engine.Session.prepareCommand(Session.java:595)
    at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1235)
    at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:76)
    at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:1154)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$2.doPrepare(StatementPreparerImpl.java:109)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:176)
    ... 135 common frames omitted`

这是课程代码:

@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode
@Entity
@Table(name = "user")
public class UserEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column( name = "ID" )
private Long id;
@Version
@Column( name = "VERSION")
private int version;
@Column( name = "USERNAME" )
private String username;
@Column( name = "PASSWORD" )
private String password;
@Column( name = "START" )
private String start;
@Column( name = "AKTIV" )
private String aktiv;
@Column( name = "USERROLE" )
private String userRole;
@OneToOne(mappedBy = "user")
private EmployeeEntity employeeEntity;
    
}

测试停止工作,引发以下错误。文件在路径:src/main/resources

Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist

这里是文件到项目中的集成:

@EnableJpaRepositories(basePackages = "com.m_landalex.dataconvert.petsistence")
@ComponentScan(basePackages = "com.m_landalex.dataconvert")
@PropertySource("classpath:application.properties")
@Configuration
public class AppServiceConfig { ... }

稍作修改后,出现新错误。感觉是Maven看不到我的资源文件夹。

    @EnableJpaRepositories(basePackages = "com.m_landalex.dataconvert.petsistence")
@ComponentScan(basePackages = "com.m_landalex.dataconvert")
@PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true)
@Configuration
public class AppServiceConfig { ... }

文件在路径:src/test/resources/db 例外:

Caused by: java.io.FileNotFoundException: class path resource [db/clean-up.sql] cannot be opened because it does not exist
Caused by: java.io.FileNotFoundException: class path resource [db/test-data.sql] cannot be opened because it does not exist

测试方法:

    @SqlGroup( { @Sql( value = "classpath:db/test-data.sql",
        config = @SqlConfig( encoding = "utf-8", separator = ";", commentPrefix = "--" ),
        executionPhase = ExecutionPhase.BEFORE_TEST_METHOD ),
        @Sql( value = "classpath:db/clean-up.sql",
        config = @SqlConfig( encoding = "utf-8", separator = ";", commentPrefix = "--" ),
        executionPhase = ExecutionPhase.AFTER_TEST_METHOD) } )
@Test
public void fetchAllTest() {
    List<AbstractObject> returnedList = defaultService.fetchAll();
    assertNotNull(returnedList);
    assertEquals(2, returnedList.size());
}

添加后

 @Bean
 public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
 }

应用程序开始运行。我的测试仍然是个问题 -

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'h2.hibernate.dialect' in value "${h2.hibernate.dialect}"

并且使用相同的配置文件

【问题讨论】:

  • 您是否对 POM 进行了任何更改?显然,项目类路径配置不正确。尝试将项目重新导入为 Maven 项目
  • @crizzis 感谢您的帮助,在此期间我已经本地化并解决了问题。 Project->Build Path->Build Path Config->Source-> src/main/resources 和 src/test/resources。转到明显的默认设置排除:**;在这两个地方我都注册了 Excluded(None) 并且它有效。我只是不明白为什么设置回滚。
  • @crizzis 而且,这发生在 Eclipse 中的所有项目中。

标签: spring eclipse hibernate maven


【解决方案1】:

user 是保留关键字,因此您必须使用方括号明确表示您的意思是名为“用户”的对象,即使用[user] 而不是user

【讨论】:

  • 我用方括号试过了,完全改变了表名 - 没有任何帮助。我忘了澄清,在这个项目的同时,所有其他项目都分崩离析。
猜你喜欢
  • 2018-06-13
  • 1970-01-01
  • 2012-06-05
  • 2017-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
相关资源
最近更新 更多