Hibernate可以插入但不能删除条目

时间:2018-11-05 15:39:25

标签: java database spring hibernate

我在尝试删除数据库表中的条目时遇到问题。 数据库的结构如下:

User ---< UserET >--- ExitTicket

ExitTicket.java

@Entity
@Table(name = "exit_ticket")
public class ExitTicketEntry implements Serializable {

private static final long serialVersionUID = 1L;

    private long id;
    private String title;
    private String dateET;
    private Set<UserET> userETs= new HashSet<UserET>();

    public ExitTicketEntry() {}

    public ExitTicketEntry(String title,String dateET) {
        this.title = title;
        this.dateET = dateET;
    }     

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY )
    @Column(name = "ticket_id")
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }

    @OneToMany(mappedBy = "exitTicketEntry", orphanRemoval=true)
    public Set<UserET> getUserETs() {
        return userETs;
    }

    public void setUserETs(Set<UserET> userETs) {
        this.userETs = userETs;
    }

    public void addUserETs(UserET userETs) {
        this.userETs.add(userETs);
    }

    //getters and setters
}

UserET.java

@Entity
@Table(name="userET")
public class UserET implements Serializable {

    //private static final long serialVersionUID = 1L;

    private long answerId;
    private User user;
    private ExitTicketEntry exitTicketEntry;
    private String answer;
    private Date dateAnswer;

    @Id
    @GeneratedValue
    @Column(name="answerId")
    public long getAnswerId() {
        return answerId;
    }
    public void setAnswerId(long answerId) {
        this.answerId = answerId;
    }

    @ManyToOne(cascade = CascadeType.ALL )
    @JoinColumn(name = "user_id")  
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "ticket_id")  
    public ExitTicketEntry getExitTicketEntry() {
        return exitTicketEntry;
    }
    public void setExitTicketEntry(ExitTicketEntry exitTicketEntry) {
        this.exitTicketEntry = exitTicketEntry;
    }

    @Column(name = "answer")
    public String getAnswer(){
        return answer;
    }
    public void setAnswer(String answer) {
        this.answer = answer;
    }

    @Column(name = "dateAnswer")
    @Temporal(TemporalType.DATE)
    public Date getDateAnswer() {
        return dateAnswer;
    }
    public void setDateAnswer(Date dateAnswer) {
        this.dateAnswer = dateAnswer;
    }

User.java

@Entity
@Table(name="USER")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private long id;
    private String user_name, user_password, user_type,user_realname;

    private Set<UserET> userETs= new HashSet<UserET>();

    public User() {
    }

    public User(String user_name,String user_password,String user_type,String user_realname) {
        this.user_name = user_name;
        this.user_password = user_password;
        this.user_type = user_type;
        this.user_realname=user_realname;
    }

    public void addExitTicketEntry(UserET group) {
        this.userETs.add(group);
    }

    @Id
    @GeneratedValue
    @Column(name="user_id")
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @OneToMany(mappedBy = "user", orphanRemoval=true)
    public Set<UserET> getUserETs() {
        return userETs;
    }

    public void setUserETs(Set<UserET> userETs) {
        this.userETs = userETs;
    }

    public void addUserETs(UserET userETs) {
        this.userETs.add(userETs);
    } ... + getters and setters

我可以使用以下功能正确插入Entry中的新ExitTicket(使用ExitTicketService.java):

public void addEntryET(ExitTicketEntry exitTicketEntry) {
    log.info("adding entry in database");
    try {
        hibernateTemplate.save(exitTicketEntry);
    }
    catch(Exception e) {
        log.info("error adding an entry in the database->"+e.toString());
    }
}

但是,当我尝试从数据库表中删除条目时,休眠删除仅执行“选择”命令,而从未执行“删除”

2018-11-05 09:28:27 INFO  AuthService:72 - entro 1 ------
Hibernate: select exitticket0_.ticket_id as ticket_i1_0_0_, exitticket0_.dateET as dateET2_0_0_, exitticket0_.title as title3_0_0_ from exit_ticket exitticket0_ where exitticket0_.ticket_id=?
Mon Nov 05 09:28:27 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
2018-11-05 09:28:27 TRACE BasicBinder:65 - binding parameter [1] as [BIGINT] - [9]
2018-11-05 09:28:27 TRACE BasicExtractor:61 - extracted value ([dateET2_0_0_] : [VARCHAR]) - [2011-05-20]
2018-11-05 09:28:27 TRACE BasicExtractor:61 - extracted value ([title3_0_0_] : [VARCHAR]) - [test1]
2018-11-05 09:28:27 TRACE CollectionType:790 - Created collection wrapper: [com.jcg.spring.hibernate.pojo.ExitTicketEntry.userETs#9]
Hibernate: select userets0_.ticket_id as ticket_i4_2_0_, userets0_.answerId as answerId1_2_0_, userets0_.answerId as answerId1_2_1_, userets0_.answer as answer2_2_1_, userets0_.dateAnswer as dateAnsw3_2_1_, userets0_.ticket_id as ticket_i4_2_1_, userets0_.user_id as user_id5_2_1_, user1_.user_id as user_id1_1_2_, user1_.user_name as user_nam2_1_2_, user1_.user_password as user_pas3_1_2_, user1_.user_realname as user_rea4_1_2_, user1_.user_type as user_typ5_1_2_ from userET userets0_ left outer join USER user1_ on userets0_.user_id=user1_.user_id where userets0_.ticket_id=?
2018-11-05 09:28:27 TRACE BasicBinder:65 - binding parameter [1] as [BIGINT] - [9]
2018-11-05 09:28:27 INFO  AuthService:99 - deleted entry with ID#=9

服务类中的删除功能:

public void deleteById(Class<?> type, long id) {
    log.info("entro 1 ------");
    ExitTicketEntry e=(ExitTicketEntry) hibernateTemplate.load(type,id);
    hibernateTemplate.delete(e);
}

我已经阅读了很多论坛,但是仍然有同样的问题,我无法删除任何条目。我想念什么吗?

0 个答案:

没有答案