休眠saveOrUpdate会话不起作用

时间:2020-09-30 20:08:18

标签: mysql spring spring-boot hibernate

我是新来的冬眠者,正在从事一个项目,当有人购买会员时,我想更新会员的到期日期。我写了一个实现AtributeConverter的类。 调用buyMembership方法后,两个打印方法都在打印正确的日期,但是数据库未更新。在控制台中,它表明仅select查询正在运行。更新查询正在执行。引用代码以获取引用

USER_LOGIN类

@Entity
@Table(name = "login")
public class User_Login {

    @Id
    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;

    @Column(name = "enabled")
    private Boolean enabled = false;
    
    @Column(name="start_date")
    private LocalDate startDate;
    
    @Column(name="end_date")
    private LocalDate expiryDate;

存储库

public void buyMembership(String months,String email) {
        
        Session currentSession = manager.unwrap(Session.class);
        
        User_Login user = currentSession.get(User_Login.class, email);
        
        LocalDate date = user.getExpiryDate();
        
        LocalDate current = LocalDate.now();
        
        if(date.compareTo(current)<0) {
            user.setStartDate(current);
            LocalDate newExpiryDate = current.plusMonths(Long.parseLong(months));
            System.out.println(newExpiryDate);
            user.setExpiryDate(newExpiryDate);
            user.setStartDate(current);
            //currentSession.saveOrUpdate(user);
        }
        else {
            LocalDate newExpiryDate = date.plusMonths(Long.parseLong(months));
            System.out.println(newExpiryDate);
            user.setExpiryDate(newExpiryDate);
            user.setStartDate(current);
            System.out.println(user.getExpiryDate());
            //currentSession.saveOrUpdate(user);
        }
        currentSession.update(user);
    
    }

属性转换器 *

@Converter(autoApply = true)
public class LocalDatePersistenceConverter implements
    AttributeConverter<LocalDate, Date> {

    @Override
    public Date convertToDatabaseColumn(LocalDate attribute) {
        if(attribute==null)
            return null;
        return java.sql.Date.valueOf(attribute);
    }

    @Override
    public LocalDate convertToEntityAttribute(java.sql.Date dbData) {

        if(dbData==null)
            return null;
        return dbData.toLocalDate();
    }
    
    
}

enter image description here

1 个答案:

答案 0 :(得分:0)

实际上,这是一个愚蠢的错误。我只是错过了将我的buyMembership服务标记为事务性的。在那之后,它就像一个魅力...