获取数据的方法,但显示“xxx”不能为空

时间:2015-03-25 06:28:22

标签: java spring hibernate jpa orm

我正在Hibernate和Spring做一个项目。我被困在如下代码中:

userTransactionRepository是user_transaction表的存储库,它已在方法调用中注入。

@Transactional
public synchronized void method() {
try {
    Iterable<UserTransaction> balances2 = userTransactionRepository.findAll
(UserTransactionPredicate.getCurrentBalance(cancelUserId), UserTransactionPredicate.orderByCreatedAtDesc());  
//got error of 'xxx' cannot be null (MysqlIntegrityViolationException)

            int count2 = 0;                 
for (UserTransaction userTransaction2 : balances2) {
                if (count2 > 0) {
                    userTransaction2.setActive(false);
                    userTransactionRepository.saveAndFlush(userTransaction2);
                }
                count2++;
            }
        } catch (Exception e) {
            System.out.println(e.getCause());
        }
}

我的UserTransaction

@Entity
@Table(name = "user_transaction")
@NamedQuery(name="UserTransaction.getUserCurrentBalance",query="from UserTransaction ut, User u WHERE ut.user.id=u.id AND u.id=:userId AND ut.active=1 ORDER BY u.createdAt", lockMode=LockModeType.PESSIMISTIC_READ)
public class UserTransaction extends Auditable {

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE, targetEntity = Operator.class)
@JoinColumn(name = "operator_id", columnDefinition = "BIGINT UNSIGNED", nullable = false)
private Operator operator;

@PrePersist
public void setOperator() {
    this.operator = VahanaContext.getInstance().operator();
}

public void setOperator(Operator operator) {
    this.operator = operator;
}
}

在调用method()时,我得到以下错误:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'operator_id' cannot be null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2825)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2459)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2376)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2360)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)
... 123 more

我希望上面声明中的 lockMode 对MysqlIntegrityException有任何影响。 什么可能是解决方案的人...任何替代方式也热烈欢迎?

1 个答案:

答案 0 :(得分:0)

你的问题是: 在进行持久化之前,运算符被设置为null。因为你有nullable = false而不是你得到这个错误。在执行persist之前尝试调试并确保设置operator(非null)。或者,如果运算符可以从nullable = false

中删除@JoinColumn(name = "operator_id", columnDefinition = "BIGINT UNSIGNED", nullable = false)