JpaRepository解析错误的表名

时间:2018-04-06 10:38:09

标签: java hibernate spring-boot spring-data-jpa

现在已经打破了我的头几天了。

我有以下 maven,多模块项目结构(遗漏了很多东西):

共同

共同的存储库 - > com.example.common.repository ---> EventRepository

common-repository-model - >实体类

[其他套餐]

混凝土包-A

web应用 - > com.example.A - > FirstApplication

[其他包,包括调用repos的服务层]

混凝土包-B

web应用 - > com.example.B - > SecondApplication

存储库 - > com.example.repository - > SecondAppRepositoryConfigA,SecondAppRepositoryConfigB

[其他包,包括调用repos的服务层]

-

事件存储库(JPA回购):

public interface EventRepository extends JpaRepository<Event, Long> {
    List<Event> findAllByExample_IdOrderByEventTime(Long exampleId);   
}

FirstApplication:

@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
@SpringBootApplication
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
@EnableJpaRepositories(basePackages = {"com.example.common.repository", ...})
@EntityScan(basePackages = {"..."})
@EnableEntityLinks
public class FirstApplication extends WebMvcConfigurerAdapter {
   [some beans, none related to repo stuff]
}

SecondApplication是入口点,包含一些bean,这些都不重要。

SecondAppRepositoryConfigA包含存储库层使用的bean和配置:

@Configuration
@EnableJpaRepositories(
    entityManagerFactoryRef = "myEntityManagerFactory",
    transactionManagerRef = "myTransactionManager",
    basePackages = {..., "com.example.common.repository"}
)
@EnableTransactionManagement
public class SecondAppRepositoryConfigA {

    @Autowired(required = false)
    private PersistenceUnitManager persistenceUnitManager;

    @Bean(name = "myJpaProperties")
    @ConfigurationProperties("example.jpa")
    @Primary
    public JpaProperties myJpaProperties() {
        return new JpaProperties();
    }

    @Bean(name = "myDataSource")
    @ConfigurationProperties(prefix = "example.datasource")
    @Primary
    public DataSource myDataSource() {
        return (DataSource) DataSourceBuilder.create().type(DataSource.class).build();
    }

    @Bean(name = "myEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean myEntityManagerFactory(JpaProperties myJpaProperties) {
        EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(myJpaProperties);
        LocalContainerEntityManagerFactoryBean emf = builder
            .dataSource(myDataSource())
            .packages("....repository.....entities",...)
            .persistenceUnit("myPersistenceUnit")
            .build();
        return emf;
    }

    @Bean(name = "myTransactionManager")
    public JpaTransactionManager myTransactionManager(@Qualifier("myEntityManagerFactory") EntityManagerFactory orderEntityManager) {
        return new JpaTransactionManager(orderEntityManager);
    }

    private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(@Qualifier("myJpaProperties") JpaProperties jpaProperties) {
        JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(jpaProperties);
        return new EntityManagerFactoryBuilder(jpaVendorAdapter, jpaProperties.getProperties(), this.persistenceUnitManager);
    }

    private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
        AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setShowSql(jpaProperties.isShowSql());
        adapter.setDatabase(jpaProperties.getDatabase());
        adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
        return adapter;
    }
}

SecondAppRepositoryConfigB也被第二个应用程序使用,但不太重要,主要用于测试:

@Configuration
public class SecondAppRepositoryConfigB {

    @Bean(name = "secondMyDataSource")
    @ConfigurationProperties(prefix = "second.datasource")
    public DataSource secondMyDataSource() {
        return (DataSource) DataSourceBuilder.create().type(DataSource.class).build();
    }

来自公共部分的实体示例:

@Data
@EntityListeners(EventListener.class)
@NamedQuery(name = "Event.findAll", query = "select e from Event e order by e.eventTime desc")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true)
@JsonSubTypes(value = {
    @JsonSubTypes.Type(name = Type.IMAGE_VALUE, value = ImageEvent.class),
    ...
})
@Entity
public abstract class Event {

    @Id
    @org.hibernate.annotations.GenericGenerator(
        ...
    )
    @GeneratedValue(...)
    private Long id;

    @Enumerated(EnumType.STRING)
    @Column(insertable = false, updatable = false)
    private Type type;

    private String displayedTime;


    ...
}

豆子等的接线似乎没问题。

但是当我在我的第一个应用程序中使用来自公共包的JPA repo时(仅使用JPA存储库并将其余部分留给spring + hibernate),hibernate查询使用正确的实体名称。即它使用_而不是camelcase。

然而,在第二个应用程序(具有我需要的更复杂查询的自定义实体管理器的应用程序)中,hibernate根本不使用camel case,生成SQL如:

select event0_.id as id2_7_, event0_.displayedTime as displayedTime3_7_, ... 
from correctschema.Event event0_ left outer join correctschema.ImageEvent event0_3_ on event0_.id= ...

事件而不是EVENT,ImageEvent而不是IMAGE_EVENT,显示时间而不是DISPLAY_TIME等。

我也在第二个应用程序日志中看到这样的东西:

o.h.cfg.annotations.EntityBinder         : Bind entity com.example.repository.entities.events.TextEvent on table TextEvent

在第一个应用程序日志中,它在表text_event&#34;上显示&#34;。

真的不知道两个模块之间的区别。使用上述方法的混合物,第三个应用程序也正常运行JPA repos。我已经考虑过设置不同的命名策略,或改变方言,但似乎没有任何效果。

提前致谢

0 个答案:

没有答案