我无法在休眠中提取结果..我想在登录时检查一个帐户,但它会引发异常:无法提取结果。
我的示例代码:
我的Pojo:
@Entity
@Table(name = "account")
public class TestAccount implements Serializable {
@Id
@Column(name="IDACCOUNT")
private Integer idAccount;
@Column(name="USERNAME")
private String username;
@Column(name="PASSWORD")
private String password;
@Column(name="ISDELETED")
private Integer isDeleted;
@ManyToOne
@JoinColumn(name="ROLE")
private TestAccountRole accountRole;
@OneToOne(mappedBy="accountInfo", cascade=CascadeType.ALL)
private TestAccountInfo accountInfo;
}
@Entity
@Table(name = "account_role")
public class TestAccountRole implements Serializable {
@Id
@Column(name="IDACCOUNT_ROLE")
private Integer idAccountRole;
@Column(name="ROLE")
private String role;
@OneToMany(mappedBy="accountRole", fetch = FetchType.LAZY)
private Set<TestAccount> accounts = new HashSet<TestAccount>(0);
}
@Entity
@Table(name = "account")
public class TestAccountInfo implements Serializable {
@Id
@Column(name="IDACCOUNT_INFO")
private Integer idAccountInfo;
@Column(name = "BIRTHDAY")
private String birthday;
@Column(name = "SEX")
private String sex;
@Column(name = "ADDRESS")
private String address;
@Column(name = "EMAIL")
private String email;
@Column(name = "PHONE")
private String phone;
@Column(name = "ISDELETED")
private Integer isDeleted;
@OneToOne
@PrimaryKeyJoinColumn
private TestAccount accountInfo;
}
我的道:
public TestAccount Authenticate(String username, String password) {
String passMD5 = AccountUtil.md5(password);
Session session = sessionFactory.getCurrentSession();
String sql = "from Account where username = ? and password = ?";
Query query = session.createQuery(sql);
query.setString(0, username);
query.setString(1, passMD5);
//query.setInteger(2, 0);
TestAccount account = (TestAccount) query.uniqueResult();
if (account != null) {
return account;
}
return null;
}
我的Db:
CREATE TABLE `account` (
`IDACCOUNT` int(11) NOT NULL AUTO_INCREMENT,
`USERNAME` varchar(45) DEFAULT NULL,
`PASSWORD` varchar(45) DEFAULT NULL,
`ISDELETED` int(11) DEFAULT NULL,
`ROLE` int(11) DEFAULT NULL,
PRIMARY KEY (`IDACCOUNT`),
KEY `ROLEACCOUNT_idx` (`ROLE`),
CONSTRAINT `ROLEACCOUNT` FOREIGN KEY (`ROLE`) REFERENCES `account_role` (`IDACCOUNT_ROLE`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE TABLE `account_info` (
`IDACCOUNT_INFO` int(11) NOT NULL AUTO_INCREMENT,
`BIRTHDAY` varchar(45) DEFAULT NULL,
`SEX` varchar(45) DEFAULT NULL,
`ADDRESS` varchar(45) DEFAULT NULL,
`EMAIL` varchar(45) DEFAULT NULL,
`PHONE` varchar(12) DEFAULT NULL,
`ISDELETED` int(11) DEFAULT NULL,
PRIMARY KEY (`IDACCOUNT_INFO`),
CONSTRAINT `ACCOUNTINFO` FOREIGN KEY (`IDACCOUNT_INFO`) REFERENCES `account` (`IDACCOUNT`) ON DELETE NO ACTION ON UPDATE NO ACTION
)
CREATE TABLE `account_role` (
`IDACCOUNT_ROLE` int(11) NOT NULL AUTO_INCREMENT,
`ROLE` varchar(45) DEFAULT NULL,
PRIMARY KEY (`IDACCOUNT_ROLE`)
)
例外
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:61)
org.hibernate.loader.Loader.getResultSet(Loader.java:2040)
org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1837)
root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'account0_.IDACCOUNTROLE' in 'field list'
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
com.mysql.jdbc.Util.getInstance(Util.java:386)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
答案 0 :(得分:0)
更改为
String sql = "from TestAccount where username = ? and password = ?";
答案 1 :(得分:0)
@Entity
@Table(name = "account")
public class TestAccountInfo implements Serializable {
TestAccountInfo中的表名不正确。应该纠正为 @Table(name =&#34; account_info&#34;)
查询还需要更改
String sql = "from TestAccount where username = ? and password = ?";