2016-08-28 24 views
0

親と子の2つのエンティティがあります。 em.remove(group)で例外を基を除去した後休止状態のカスケード削除が機能しません

 


    @Entity 
    @Table(name = "university_group") 
    public class Group { 
     @Id 
     @GeneratedValue(strategy = GenerationType.SEQUENCE) 
     private long id; 
     private String name; 

     @OneToMany(mappedBy = "group", fetch = FetchType.LAZY, cascade = CascadeType.ALL, 
orphanRemoval = true) private Set<Student> students = new HashSet<>(); // getters, setters, constructor, equals+hashcode ... } @Entity @Table(name = "student") public class Student { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private long id; private String name; private String password; private String email; @ManyToOne(optional = false) private Group group; // getters, setters, constructor, equals+hashcode ... }

がスローされます。それは必要がありますがそれは休止状態のように思える

javax.persistence.PersistenceException: 
    org.hibernate.exception.ConstraintViolationException ... 
    org.hibernate.engine.jdbc.spi.SqlExceptionHelper: ERROR: 
    UPDATE or DELETE in table "university_group" breaks foreign key constraint "fk_20su8ubuwt33je1a3ygal7wd6" of table "student" 

は、持続性プロバイダによってグループの前に学生を削除されていません。もちろん、私はDBカスケードを有効にすることができますが、私は問題を解決する方がよいでしょう。 アイデア休止4.3.11スプリングコンフィグ



    @Configuration 
    @EnableTransactionManagement 
    @PropertySource({"classpath:db.properties"}) 
    public class PersistenceContext { 

     private static final String BASE_MODEL_SCAN_PACKAGE = "com.chiefhelpsystem.model"; 

     @Value("${db.driverClassName}") 
     private String dbClassName; 
     @Value("${db.url}") 
     private String dbUrl; 
     @Value("${db.username}") 
     private String dbUserName; 
     @Value("${db.password}") 
     private String dbPassword; 

     @Bean 
     DataSource dataSource() { 
      BasicDataSource ds = new BasicDataSource(); 
      ds.setMaxIdle(20); 
      ds.setMinIdle(0); 
      ds.setMaxActive(20); 

      ds.setDriverClassName(dbClassName); 
      ds.setUrl(dbUrl); 
      ds.setUsername(dbUserName); 
      ds.setPassword(dbPassword); 

      return ds; 
     } 

     @Bean 
     PlatformTransactionManager transactionManager() { 
      return new JpaTransactionManager(); 
     } 

     @Bean(destroyMethod = "destroy") 
     LocalContainerEntityManagerFactoryBean emf() { 
      LocalContainerEntityManagerFactoryBean emFactory = 
        new LocalContainerEntityManagerFactoryBean(); 

      HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); 
      jpaVendorAdapter.setDatabase(Database.POSTGRESQL); 
      jpaVendorAdapter.setGenerateDdl(true); 
      jpaVendorAdapter.setShowSql(true); 
      emFactory.setDataSource(dataSource()); 
      emFactory.setPackagesToScan(BASE_MODEL_SCAN_PACKAGE); 
      emFactory.setJpaVendorAdapter(jpaVendorAdapter); 
      emFactory.setJpaProperties(jpaProps()); 
      emFactory.setPersistenceProvider(new HibernatePersistenceProvider()); 
      return emFactory; 
     } 

     private Properties jpaProps() { 
      Properties properties = new Properties(); 
      properties.setProperty("format_sql", "true"); 
      return properties; 
     } 

    } 

によってEntityManagerを設定さ

、スプリング4.3.2

+1

削除のコードを表示します。 –

+0

em.delete(エンティティ); –

答えて

0

問題が誤っhachcode()メソッドを実現していました。ソースから削除するとすぐに、「管理されていない削除」問題がHibernate TRACEログに記録され、さらに修正する必要があります。