hibernate:删除一对多关系中的实体

时间:2015-11-04 21:14:13

标签: java hibernate jpa

我遇到以下错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`spindledb`.`section`, CONSTRAINT `FK_ftoru9cp83n512p9is8x3vo53` FOREIGN KEY (`scenario_id`) REFERENCES `scenario` (`scenario_id`))

以下是我的课程:

情景:

@Entity
@Table(name = "scenario")
public class Scenario {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "scenario_id")
private int id;

@Column(name = "title", nullable = false)
private String title;

@NotNull
@DateTimeFormat(pattern = "dd/MM/yyyy")
@Column(name = "creation_date", nullable = false)
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
private LocalDate creationDate;

@ManyToOne
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name = "id", nullable = false)
private User user;

@OneToMany(mappedBy = "scenario", orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
private Set<Plot> plotList = new HashSet<Plot>();

@OneToMany(mappedBy = "scenario", orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
private Set<Character> characterList = new HashSet<Character>();

@OneToMany(mappedBy = "scenario", cascade=CascadeType.ALL, orphanRemoval = true)
@LazyCollection(LazyCollectionOption.FALSE)
@OrderBy("sequence ASC")
private Set<Section> sectionList = new HashSet<Section>();

部分:

@Entity
@Table(name = "section")
public class Section {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "section_id")
private int id;

@Size(min = 4, max = 50)
@NotNull
@Column(name = "name")
private String name;

@NotNull
@Column(name = "type")
private String type = SectionType.TEXT.getSectionType();

@Column(name = "visibility")
private boolean visibility;

@NotNull
@Column(name = "sequence")
private int sequence;

@ManyToOne (cascade=CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
@JoinColumn(name = "scenario_id", nullable = false)
private Scenario scenario;

控制器:

    @RequestMapping(value = { "/delete-{id}-scenario" }, method = RequestMethod.GET)
public String deleteScenario(@PathVariable int id) {

    scenarioService.deleteScenarioById(id);
    return "redirect:/home";
}

情景服务:

@Service("scenarioService")
@Transactional
public class ScenarioServiceImpl implements ScenarioService {

@Autowired
private ScenarioDao dao;

@Override
public Scenario findById(int id) {
    return dao.findById(id);
}

@Override
public void saveScenario(Scenario scenario) {
    dao.saveScenario(scenario);

}

public void updateScenario(Scenario scenario) {
    Scenario entity = dao.findById(scenario.getId());
    if(entity!=null){
        entity.setTitle(scenario.getTitle());
        entity.setCreationDate(scenario.getCreationDate());            
    }
}

@Override
public void deleteScenarioById(int id) {
    dao.deleteScenarioById(id);

}

@Repository("scenarioDao")
public class ScenarioDaoImpl extends AbstractDao<Integer, Scenario> implements ScenarioDao {

@Override
public Scenario findById(int id) {
    return getByKey(id);
}

@Override
public void saveScenario(Scenario scenario) {
    persist(scenario);

}

@Override
public void deleteScenarioById(int id) {

    Query query = getSession().createSQLQuery("delete from scenario where id = :id");
    query.setString("id", ""+id);
    query.executeUpdate();
}

据我所知,问题是可能存在一个没有场景就不能存在的部分。但是现在数据库中的部分表是空的,我仍然无法删除Scenario。感谢您的建议

2 个答案:

答案 0 :(得分:0)

cascade=CascadeType.ALL@ManyToOne中的所有class Scenario关系一起使用,因为如果您要从数据库中删除任何方案,则不得在数据库中的任何位置引用它。

删除的另一种方法是。

Serializable id = new Long(1); //your id
Object persistentInstance = session.load(Scenario.class, id);
if (persistentInstance != null) {
    session.delete(persistentInstance);
}

答案 1 :(得分:0)

通过Query删除实体会绕过您通过注释添加的任何级联设置。

我建议首先通过id找到实体,然后删除实体对象:

Object scenario = session.load(Scenario.class, id);
if (scenario != null) {
    session.delete(scenario);
}