2015-11-04 118 views
0

我有一個以下錯誤:休眠:刪除實體在一個一對多的關係

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。感謝您的建議

回答

0

使用cascade=CascadeType.ALLclass Scenario所有@ManyToOne的關係,因爲如果你打算從數據庫中刪除任何方案它決不能引用數據的基礎上任何地方。

另一種方法是刪除。

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

遺憾的是沒有幫助 – TergOfSky

+0

通過查詢刪除實體將繞過任何級聯設定。所以這真的沒有幫助。 – Ish

+0

使用以下技術刪除已定義的 http://www.codejava.net/frameworks/hibernate/hibernate-basics-3-ways-to-delete-an-entity-from-the-datastore Serializable id = new Long (1); //你的ID 對象persistentInstance = session.load(Scenario.class,id); (persistentInstance!= null){ session.delete(persistentInstance); } –

0

通過Query刪除的實體將繞過您通過註釋把任何級聯設置。

我建議先找到實體的ID,然後刪除實體對象:

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