【问题标题】:Error creating bean unsatisfied dependency expressed through field sessionFactory创建通过字段 sessionFactory 表示的 bean 不满足的依赖项时出错
【发布时间】:2020-05-07 07:12:24
【问题描述】:

我遇到错误,它说SECU 表中不存在列。但问题是我真的不需要这个表中的这个列,因为我只需要NONSECU 表中的这个列,它只在NONSECU 类中引用。

有没有办法在 getbean 方法或其他地方摆脱这个?

`[main] WARN  org.springframework.context.annotation.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'IsmaImp': Unsatisfied dependency expressed through field 'sessionFactory': Error creating bean with name 'sessionFactory' defined in baag.betl.dbimporter.esmatrans.SpringConfiguration: Invocation of init method failed; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column [preTradLrgInScaleThrshld] in table [SECU]; nested exception is org.springframework.beans.factory.BeanCreationException: 

@配置 @ComponentScan("baag.betl.dbimporter.Ismatr") @EnableTransactionManagement 公共类 SpringConfiguration {

@Autowired
private Environment env;

@Bean
public DataSource dataSource() {
    return createDatasource("dwh.");
}

private DriverManagerDataSource createDatasource(String propertyPrefix) {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(env.getRequiredProperty(propertyPrefix + "driver"));
    dataSource.setUrl(env.getRequiredProperty(propertyPrefix + "jdbc"));
    dataSource.setUsername(env.getRequiredProperty(propertyPrefix + "user"));
    dataSource.setPassword(env.getRequiredProperty(propertyPrefix + "pwd"));
    return dataSource;
}

@Bean
public Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.put(AvailableSettings.DIALECT, env.getRequiredProperty("hibernate.dialect"));
    properties.put(AvailableSettings.SHOW_SQL, env.getRequiredProperty("hibernate.show_sql"));
    properties.put(AvailableSettings.STATEMENT_BATCH_SIZE, env.getRequiredProperty("hibernate.batch.size"));
    properties.put(AvailableSettings.HBM2DDL_AUTO, env.getRequiredProperty("hibernate.hbm2ddl.auto"));
    properties.put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, env.getRequiredProperty("hibernate.current.session.context.class"));
    return properties;
}

@Bean
public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    sessionFactory.setAnnotatedClasses(Secu.class, NonSecu.class);
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
}

@Bean
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
    HibernateTransactionManager txManager = new HibernateTransactionManager();
    txManager.setSessionFactory(sessionFactory);
    return txManager;
}

}

【问题讨论】:

  • 你能添加你的实体类吗
  • @Andrew 请显示您的实体类以便更好地理解
  • 实体类意味着我在哪里为所有实体创建了get和set方法?
  • 只显示 Secu 和 NonSecu 类。
  • 添加实体类

标签: java spring exception javabeans


【解决方案1】:

如果您想忽略某些字段的列映射,只需将它们声明为 Transient

像这样:


@Entity
@Table(name = "SECU")
public class SECU implements Serializable {

    @Transient
    protected BigDecimal preTradLrgInScaleThrshld;

此字段不会持久存在,并且在加载后始终具有空值。

【讨论】:

  • 但它真的能避免表中的错误缺失列吗?如果我删除了受保护的 BigDecimal preTradLrgInScaleThrshld 行;然后它给了我同样的错误
  • 该字段是否存在于 NonSecu 类中?
  • 是的,实际上我只需要在 NonSecu 表中使用此字段,所以我已将它们添加到 Nonsecu 类中
  • 我看到protected 你有一些实体继承吗?一些bean继承了Secu.class
  • 那么为什么要protected?你试过@Transient 吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 2020-04-04
  • 1970-01-01
  • 2019-02-22
  • 2019-10-11
  • 2021-07-23
  • 2020-11-25
  • 2017-05-15
相关资源
最近更新 更多