Roughly speaking, there are 2 entities.
@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 ... } While deleting a group with em.remove(group) thrown
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException ... org.hibernate.engine.jdbc.spi.SqlExceptionHelper: ОШИБКА: UPDATE или DELETE в таблице "university_group" нарушает ограничение внешнего ключа "fk_20su8ubuwt33je1a3ygal7wd6" таблицы "student" Подробности: На ключ (id)=(7630) всё ещё есть ссылки в таблице "student". It seems that Hiberneyt does not want to first remove students by means of Persistence Provider. I do not want to delete using DB cascading tools, since why the Persistence Provider does not.
Configured with EntityManager Spring.
@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; } } Hibernate 4.3.11, Spring 4.3.2