我有一个实体吧:
@OneToMany(cascade = CascadeType.ALL,mappedBy = "bar") private Set<Foo> fooSet;
一个实体Foo:
@ManyToOne(optional = false) @JoinColumn(name = "bar_id") private Bar bar;
Hibernate在foo.bar上创建外键约束 – > bar.id但它没有指定ON DELETE CASCADE.为什么不?有没有办法实现它?
或者我可以在DB中手动添加ON DELETE CASCADE(并禁用DDL生成),这是一个好习惯吗?而且,我是否必须以某种方式修改我的代码以让Hibernate知道数据库会自动删除相关记录?
谢谢.
更新 – 这是我的JPA / Hibernate / Postgresql配置:
<bean id="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy"> <constructor-arg> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.postgresql.Driver" /> <property name="url" value="jdbc:postgresql://localhost:5432/my_db" /> <property name="username" value="my_username" /> <property name="password" value="my_password" /> </bean> </constructor-arg> </bean> <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="databasePlatform" value="org.hibernate.dialect.PostgresqlDialect" /> <property name="showsql" value="true" /> <property name="generateDdl" value="true" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerfactorybean"> <property name="jpaVendorAdapter" ref="jpaAdapter" /> <property name="jpaProperties"> <props> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="dataSource" ref="dataSource" /> </bean>
更新2 – 阐明了我的意思:创建了外键约束,但我想知道为什么它没有指定ON DELETE CASCADE(相应地更改了原始问题)