当将它作为依赖项导入到另一个项目中时,为什么在此Spring存储库上看到不同的行为?

时间:2019-07-16 00:06:24

标签: spring postgresql hibernate maven jpa

当我独立运行项目与将项目添加为maven依赖项时,与Spring带注释的存储库相比,我看到了不同的行为。 the Spring CrudRepository上的delete方法似乎是我看到奇怪行为的地方。

我认为这是我的界面和相关实体的相关部分:

@Repository
public interface JpaProfileRepo extends ProfileRepo, JpaRepository<UserProfileEntity, Long>, GsonProvider {

    @Transactional
    default void removeProfile(Profile profile){
        if(profile == null){
            throw new MissingKeyDataException("Missing valid ProfileModel object during removeProfile");
        }
        UserProfileEntity entity = findEntityByKeys(profile);
        if (entity == null) throw new DoesNotExistException();

        try{
            this.delete(entity);
        } catch (EmptyResultDataAccessException ex) {
            throw new DoesNotExistException();
        }
    }
}

public interface ProfileRepo {
    void removeProfile(Profile profile);
}
@Entity
@Table(name="user_profiles")
public class UserProfileEntity {
    @Id
    @SequenceGenerator(name = "user_profiles_id_seq", sequenceName = "user_profiles_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_profiles_id_seq")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private UserEntity user;

    @NotNull
    @Column(name = "permissions", columnDefinition="TEXT")
    private String permissions;
}
@Entity
@Table(name="users", uniqueConstraints ={
        @UniqueConstraint(columnNames={"email_address"})
})
public class UserEntity {
    @Id
    @SequenceGenerator(name = "users_id_seq", sequenceName = "users_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "users_id_seq")
    private Long id;

    @NotNull
    @Column(name = "email_address", updatable = false)
    private String emailAddress;

    @Column(name = "preferences", columnDefinition="TEXT")
    private String preferences;

在原始项目上通过removeProfile方法进行调试时,在执行user_profiles之前和之后,我都会在this.delete(entity)表上运行查询。我可以确认传递到removeProfile方法的实体的表条目在该行运行之前已存在,并在该行运行之后已删除。

当我从父项目中调用相同的代码时调试该代码时,对于运行于removeProfile方法之前和之后的实体的表项是否存在,我将运行相同的查询。在这种情况下,表条目不会被删除,但是我看不到抛出任何异常或任何其他错误-该行只是没有被删除,就像我期望的那样。

我要提到的另一个问题是我正在使用IntelliJ,并且在两个项目中都配置了本地数据库连接。当我从原始项目的Postgres控制台运行select * from pg_catalog.pg_tables;时,我看到user_profilesusers表又回到数据库的表列表中。在父项目上运行相同的查询时,在数据库表列表中看不到user_profilesusers表,只看到默认的postgres表。基于从两个单独的项目连接到同一本地数据库,是否可能会出现某种错误?如果是这种情况,我希望父项目实际上无法连接到数据库,而不是显示成功的连接,而是悄无声息地无法运行数据库查询。

作为参考,这里是与两个项目属性文件相关的所有数据库:

原始项目:

spring.jpa.database=default

spring.jpa.generate-ddl=true
spring.jpa.show-sql=true

datasource.platform=postgres
datasource.jdbc-url=jdbc:postgresql://localhost:5432/database
sql-dialect=org.hibernate.dialect.PostgreSQLDialect
datasource.hbm2ddl.auto=

datasource.username=user
datasource.password=   

父项目:

spring.jpa.database=default

spring.jpa.generate-ddl=true
spring.jpa.show-sql=true

datasource.platform=postgres
datasource.jdbc-url=jdbc:postgresql://localhost:5432/database
sql-dialect=org.hibernate.dialect.PostgreSQLDialect
datasource.hbm2ddl.auto=

datasource.username=user
datasource.password=

在我看来,这个问题可能与Spring相关,与JPA / Hibernate相关,与Maven相关,与Postgres相关,与IntelliJ相关,或者是一些令人愉快的组合。关于我在这里缺少什么的任何想法?

作为参考,以下是我使用的版本:
  春季:2.0.3
  邮编:9.4-1206-jdbc42
  Maven:2.7
  这是我们的休眠/ JPA依赖关系:

<dependency>
   <groupId>org.hibernate.javax.persistence</groupId>
   <artifactId>hibernate-jpa-2.1-api</artifactId>
   <version>1.0.0.Final</version>
</dependency>
<dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-jpa</artifactId>
   <version>2.0.5.RELEASE</version>
</dependency>

这是我拥有的数据库配置类:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManager",
        transactionManagerRef = "transactionManager",
        basePackages = {"com.repositories"}
)
public class DbConfig {
    @Value("${datasource.hbm2ddl.auto}")
    private String ddlAuto;
    @Value("${sql-dialect}")
    private String dialect;


    @Bean
    @ConfigurationProperties(prefix="datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public EntityManagerFactoryBuilder entityManagerFactoryBuilder() {
        return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), new HashMap<>(), null);
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManager(
            EntityManagerFactoryBuilder builder,
            @Qualifier("dataSource") DataSource dataSource)
    {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("hibernate.dialect",dialect);
        properties.put("hibernate.hbm2ddl.auto",ddlAuto);

        return builder
                .dataSource(dataSource)
                .persistenceUnit("object")
                .properties(properties)
                .packages("com.entities")
                .build();
    }

    @Bean
    public PlatformTransactionManager transactionManager(
            @Qualifier("entityManager") EntityManagerFactory
                    entityManagerFactory
    ) {
        return new JpaTransactionManager(entityManagerFactory);
    }

}

0 个答案:

没有答案