我尝试在Administrator.java类中添加一些映射,当我点击登录按钮时,无论用户登录的是什么或者凭据是否正确,我都会给出错误。在添加额外功能之前,一切正常。
堆栈追踪:
[error] o.j.ConnectionLogger - java.sql.Connection.prepareStatement:
throws exception: org.h2.jdbc.JdbcSQLException: Ambiguous column name "ID"; SQL statement:
select t0.role c0, t0.email c1, t0.role c2, t0.name c3, t0.password c4, t1.id c5, t0.department c6, t0.street1 c7, t0.street2 c8, t0.town c9, t0.post_code c10, t1.id c11, t2.id c12 from user t0 left outer join stock_basket t1 on t1.administrator_email = t0.email left outer join basket t1 on t1.customer_email = t0.email left outer join credit_card t2 on t2.customer_email = t0.email where t0.email = ? and t0.password = ? [90059-191]
org.h2.jdbc.JdbcSQLException: Ambiguous column name "ID"; SQL statement:
select t0.role c0, t0.email c1, t0.role c2, t0.name c3, t0.password c4, t1.id c5, t0.department c6, t0.street1 c7, t0.street2 c8, t0.town c9, t0.post_code c10, t1.id c11, t2.id c12 from user t0 left outer join stock_basket t1 on t1.administrator_email = t0.email left outer join basket t1 on t1.customer_email = t0.email left outer join credit_card t2 on t2.customer_email = t0.email where t0.email = ? and t0.password = ? [90059-191]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.expression.ExpressionColumn.mapColumn(ExpressionColumn.java:124)
at org.h2.expression.ExpressionColumn.mapColumns(ExpressionColumn.java:94)
at org.h2.expression.Alias.mapColumns(Alias.java:46)
at org.h2.command.dml.Select.mapColumns(Select.java:1228)
at org.h2.command.dml.Select.init(Select.java:827)
at org.h2.command.Parser.parseSelect(Parser.java:1693)
at org.h2.command.Parser.parsePrepared(Parser.java:443)
[error] application -
! @73gmcki3k - Internal server error, for (POST) [/login] ->
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:280)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:98)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.util.concurrent.CompletionException: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at java.util.concurrent.CompletableFuture.encodeThrowable(Unknown Source)
at java.util.concurrent.CompletableFuture.completeThrowable(Unknown Source)
at java.util.concurrent.CompletableFuture.uniApply(Unknown Source)
at java.util.concurrent.CompletableFuture$UniApply.tryFire(Unknown Source)
at java.util.concurrent.CompletableFuture.postComplete(Unknown Source)
at java.util.concurrent.CompletableFuture.completeExceptionally(Unknown Source)
at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:21)
at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:18)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:63)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at play.data.Form.bind(Form.java:425)
at play.data.Form.bindFromRequest(Form.java:234)
at controllers.security.LoginCtrl.loginSubmit(LoginCtrl.java:35)
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$4$$anonfun$apply$4.apply(Routes.scala:900)
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$4$$anonfun$apply$4.apply(Routes.scala:900)
at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:157)
at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:156)
at play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3$$anon$1.invocation(HandlerInvoker.scala:136)
at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:73)
at play.http.HttpRequestHandler$1.call(HttpRequestHandler.java:54)
Caused by: java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at play.data.Form.bind(Form.java:421)
at play.data.Form.bindFromRequest(Form.java:234)
at controllers.security.LoginCtrl.loginSubmit(LoginCtrl.java:35)
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$4$$anonfun$apply$4.apply(Routes.scala:900)
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$4$$anonfun$apply$4.apply(Routes.scala:900)
at play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:157)
Caused by: javax.persistence.PersistenceException: Query threw SQLException:Ambiguous column name "ID"; SQL statement:
select t0.role c0, t0.email c1, t0.role c2, t0.name c3, t0.password c4, t1.id c5, t0.department c6, t0.street1 c7, t0.street2 c8, t0.town c9, t0.post_code c10, t1.id c11, t2.id c12 from user t0 left outer join stock_basket t1 on t1.administrator_email = t0.email left outer join basket t1 on t1.customer_email = t0.email left outer join credit_card t2 on t2.customer_email = t0.email where t0.email = ? and t0.password = ? [90059-191]
Bind values:[null]
Query was:
select t0.role c0, t0.email c1, t0.role c2, t0.name c3, t0.password c4, t1.id c5, t0.department c6, t0.street1 c7, t0.street2 c8, t0.town c9, t0.post_code c10, t1.id c11, t2.id c12 from user t0 left outer join stock_basket t1 on t1.administrator_email = t0.email left outer join basket t1 on t1.customer_email = t0.email left outer join credit_card t2 on t2.customer_email = t0.email where t0.email = ? and t0.password = ?
at com.avaje.ebeaninternal.server.query.CQuery.createPersistenceException(CQuery.java:673)
at com.avaje.ebeaninternal.server.query.CQuery.createPersistenceException(CQuery.java:653)
at com.avaje.ebeaninternal.server.query.CQueryEngine.findMany(CQueryEngine.java:324)
at com.avaje.ebeaninternal.server.query.DefaultOrmQueryEngine.findMany(DefaultOrmQueryEngine.java:94)
at com.avaje.ebeaninternal.server.core.OrmQueryRequest.findList(OrmQueryRequest.java:347)
at com.avaje.ebeaninternal.server.core.DefaultServer.findList(DefaultServer.java:1388)
at com.avaje.ebeaninternal.server.core.DefaultServer.findUnique(DefaultServer.java:1138)
at com.avaje.ebeaninternal.server.querydefn.DefaultOrmQuery.findUnique(DefaultOrmQuery.java:1104)
at com.avaje.ebeaninternal.server.expression.DefaultExpressionList.findUnique(DefaultExpressionList.java:352)
at models.users.User.authenticate(User.java:60)
Caused by: org.h2.jdbc.JdbcSQLException: Ambiguous column name "ID"; SQL statement:
select t0.role c0, t0.email c1, t0.role c2, t0.name c3, t0.password c4, t1.id c5, t0.department c6, t0.street1 c7, t0.street2 c8, t0.town c9, t0.post_code c10, t1.id c11, t2.id c12 from user t0 left outer join stock_basket t1 on t1.administrator_email = t0.email left outer join basket t1 on t1.customer_email = t0.email left outer join credit_card t2 on t2.customer_email = t0.email where t0.email = ? and t0.password = ? [90059-191]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.expression.ExpressionColumn.mapColumn(ExpressionColumn.java:124)
at org.h2.expression.ExpressionColumn.mapColumns(ExpressionColumn.java:94)
at org.h2.expression.Alias.mapColumns(Alias.java:46)
at org.h2.command.dml.Select.mapColumns(Select.java:1228)
at org.h2.command.dml.Select.init(Select.java:827)
at org.h2.command.Parser.parseSelect(Parser.java:1693)
at org.h2.command.Parser.parsePrepared(Parser.java:443)
用户类:
package models.users;
import java.util.*;
import javax.persistence.*;
import play.data.format.*;
import play.data.validation.*;
import com.avaje.ebean.*;
//https://www.playframework.com/documentation/2.2.x/JavaGuide4
// Product entity managed by Ebean
@Entity
// specify mapped table name
@Table(name = "user")
// Map inherited classes to a single table
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
// Discriminator column used to define user type
@DiscriminatorColumn(name = "role")
// This user type is user
@DiscriminatorValue("user")
public class User extends Model {
//@Id
//@GeneratedValue(strategy = GenerationType.IDENTITY)
//public Long id;
@Constraints.Required
@Id
private String email;
@Column(insertable=false, updatable=false)
private String role;
@Constraints.Required
private String name;
@Constraints.Required
private String password;
// Default constructor
public User() {
}
// Constructor to initialise object
public User(String email, String role, String name, String password) {
this.email = email;
this.role = role;
this.name = name;
this.password = password;
}
//Generic query helper for entity User with unique id String
public static Finder<String,User> find = new Finder<String,User>(User.class);
// Static method to authenticate based on username and password
// Returns user object if found, otherwise NULL
public static User authenticate(String email, String password) {
// If found return the user object with matching username and password
return find.where().eq("email", email).eq("password", password).findUnique();
}
// Check if a user is logged in (by id - email address)
public static User getLoggedIn(String id) {
if (id == null) {
return null;
} else {
// Find user by id and return object
return find.byId(id);
}
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setRole(String role) {
this.role = role;
}
public String getRole() {
return role;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
管理员课程:
package models.users;
import models.products.Category;
import models.products.Review;
import models.shopping.Basket;
import models.shopping.ShopOrder;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import models.stock.StockBasket;
import models.stock.StockOrder;
import play.data.validation.*;
@Entity
// This is a User of type admin
@DiscriminatorValue("admin")
// Administrator inherits from the User class
public class Administrator extends User {
@OneToOne(mappedBy = "administrator", cascade = CascadeType.ALL)
private StockBasket basket;
@OneToMany(mappedBy = "administrator", cascade = CascadeType.ALL)
private List<StockOrder> orders;
public Administrator() {
}
public Administrator(String email, String role, String name, String password)
{
super(email, role, name, password);
}
public StockBasket getBasket() {
return basket;
}
public void setBasket(StockBasket basket) {
this.basket = basket;
}
public List<StockOrder> getOrders() {
return orders;
}
public void setOrders(List<StockOrder> orders) {
this.orders = orders;
}
}
答案 0 :(得分:1)
您在查询中为stock_basket和basket提供了相同的别名(t1)。
将一个别名更改为t3,这应该有效:
select t0.role c0, t0.email c1, t0.role c2, t0.name c3, t0.password c4, t1.id c5,
t0.department c6, t0.street1 c7, t0.street2 c8, t0.town c9, t0.post_code c10, t3.id c11, t2.id c12
from user t0
left outer join stock_basket t1 on t1.administrator_email = t0.email
left outer join basket t3 on t3.customer_email = t0.email
left outer join credit_card t2 on t2.customer_email = t0.email
这是更容易使用可以识别回表名而不是t1,t2等的别名的原因之一。