我正在使用Dropwizard java示例实现HERE
我只用SQL server替换数据库进行了一次更改。通过在sql server中创建包含列的表,它也运行良好。
向localhost发布请求:3000 / auth / signup获取正确的数据。
AuthController.java:
@Path("signup")
@UnitOfWork
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response signup(@Valid final User user, @Context final HttpServletRequest request)
throws JOSEException {
System.out.print(user);
user.setPASSWORD(PasswordService.hashPassword(user.getPASSWORD()));
final User savedUser = dao.save(user);
final Token token = AuthUtils.createToken(request.getRemoteHost(), savedUser.getUSER_ID());
return Response.status(Status.CREATED).entity(token).build();
}
此处的帖子数据也是正确的,也是在userdao中检查的。
UserDAO.java:
public User save(User user) {
System.out.print(user);
return persist(user);
}
此处也更正数据。
User.java:
@Entity
@Table(name = "TBLLOGINUSER")
@NamedQueries({
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
@NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE u.USER_NAME = :email")
})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long USER_ID;
@Email
@Column(name = "USER_NAME")
private String USER_NAME;
@Column(name = "PASSWORD")
private String PASSWORD;
@Column(name = "LAST_LOGIN")
private String LAST_LOGIN;
@Column(name = "STATUS")
private String STATUS;
public Long getUSER_ID() {
return USER_ID;
}
public String getUSER_NAME() {
return USER_NAME;
}
public String getSTATUS() {
return STATUS;
}
public String getLAST_LOGIN() {
return LAST_LOGIN;
}
@JsonIgnore
public String getPASSWORD() {
return PASSWORD;
}
public void setUSER_NAME(final String email) {
this.USER_NAME = email;
}
public void setPASSWORD(final String password) {
this.PASSWORD = password;
}
public void setLAST_LOGIN(final String name) {
this.LAST_LOGIN = name;
}
public void setSTATUS(final String name) {
this.STATUS = name;
}
public void setUSER_ID(final Long name) {
this.USER_ID = name;
}
@JsonIgnore
public int getSignInMethodCount() throws IllegalArgumentException,IllegalAccessException,
NoSuchFieldException, SecurityException {
int count = 0;
if (this.getPASSWORD() != null) {
count++;
} return count;
}
}
但是日志记录就是这样的。
DEBUG [2016-07-08 09:29:31,472] org.hibernate.SQL: /* insert com.example.helloworld.core.User */ insert into TBLLOGINUSER (LAST_LOGIN, PASSWORD, STATUS, USER_NAME) values(?, ?, ?, ?)
WARN [2016-07-08 09:29:31,480] org.hibernate.engine.jdbc.spi.SqlExceptionHelper: SQL Error: 0, SQLState: 22001
ERROR [2016-07-08 09:29:31,480] org.hibernate.engine.jdbc.spi.SqlExceptionHelper: Data truncation
WARN [2016-07-08 09:29:31,480] org.hibernate.engine.jdbc.spi.SqlExceptionHelper: SQL Error: 8152, SQLState: 22001
ERROR [2016-07-08 09:29:31,480] org.hibernate.engine.jdbc.spi.SqlExceptionHelper: String or binary data would be truncated.
ERROR [2016-07-08 09:29:31,486] org.hibernate.AssertionFailure: HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: null id in com.example.helloworld.core.User entry (don't flush the Session after an exception occurs)
INFO [2016-07-08 09:29:31,488] org.hibernate.engine.internal.StatisticalLoggingSessionEventListener: Session Metrics {
70528 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
1983808 nanoseconds spent preparing 1 JDBC statements;
4599920 nanoseconds spent executing 1 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
3196256 nanoseconds spent executing 1 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
WARN [2016-07-08 09:29:31,507] org.eclipse.jetty.servlet.ServletHandler:! org.hibernate.AssertionFailure: null id in com.example.helloworld.core.User entry (don't flush the Session after an exception occurs)
知道为什么将空值发送到db ????