【问题标题】:Why ormlite don't get the correct table name by jpa Table annotation?为什么 ormlite 没有通过 jpa Table 注释得到正确的表名?
【发布时间】:2014-06-27 09:23:24
【问题描述】:

这是我得到的。Table 注释将表名设置为cbhistory_article,但 ormlite 的TableInfo 获取表名是article

我怎样才能得到正确的东西?

com.j256.ormlite.table.DatabaseTableConfig#extractTableName 会发现,com.j256.ormlite.misc.JavaxPersistence#getEntityName 只获取实体名而不是表名。但为什么呢?

根据this,我认为考虑使用Table 名称更好或同时使用TableEntity 名称。

【问题讨论】:

  • 你能显示有问题的代码吗?你是如何定义实体的?

标签: java ormlite


【解决方案1】:

@Table注解将表名设置为cbhistory_article,但ormlite的TableInfo获取表名是article。

您没有提供用于定义实体的代码,但我认为它类似于:

@Entity
@Table(name = "cbhistory_article")
public class Article {
   ...

很遗憾,此时ORMLite 不支持@Table 注解。我已将此代码添加到主干,它将在版本 4.49 中。对不起错过了。但是,现在它只收听name 字段。您需要更多其他领域的支持吗?

要使其工作,请使用 @Entity 注释上的 name 字段:

@Entity(name = "cbhistory_article")
public class Article {
   ...

【讨论】:

    【解决方案2】:

    这是我的工作方式。

    @Inject
    private void initDao(DataSource dataSource, @Named("jdbc.url") String url) throws SQLException
    {
        setConnectionSource(new DataSourceConnectionSource(dataSource, url));
    
        DatabaseTableConfig<T> config = DatabaseTableConfig.fromClass(getConnectionSource(), dataClass);
        config.setTableName(getTableName());
        setTableConfig(config);
    
        initialize();
    }
    
    private String getTableName()
    {
        Table table = dataClass.getAnnotation(Table.class);
        if (table == null || Strings.isNullOrEmpty(table.name()))
            return DatabaseTableConfig.extractTableName(dataClass);
    
        return table.name();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-19
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多