我想知道是否有可能使用@PrePersist,并@PreUpdate与Ebean玩吧!2.0.如果是这样,该功能如何激活.我已经看到一个月前有一个拉取请求添加此功能,但我无法在Play 2.0上进行此操作.
谢谢
我正在使用PlayFramework,我真的很喜欢它.当我想从表中获取数据时,例如我有一个user表,我使用以下语法:
List<User> users = User.find.where().eq("email", email).findList();
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我得到用户对象时,我有一个id列.使用该id值,我可以映射到其他表,并且id这些表的's'可以映射到更多的表,因此跨多个表连接的基本概念.是否有任何示例或地方我可以阅读它描述如何使用上述语法实现它?
我试图找到自己而不能,只有这样我才能想到它是使用直接sql与准备好的语句,我宁愿不做.
我有这个枚举:
public enum DocumentTypes {
PDF("PDF Document"), JPG("Image files (JPG)"), DOC("Microsoft Word documents");
private final String displayName;
DocumentTypes(final String display) {
this.displayName = display;
}
@Override
public String toString() {
return this.displayName;
}
}
Run Code Online (Sandbox Code Playgroud)
这样的模型:
@Entity
@Table(name = "documents")
public class Document extends Model {
@Id
public Long id;
@Constraints.Required
@Formats.NonEmpty
@Enumerated(EnumType.STRING)
@Column(length=20, nullable=false)
public DocumentTypes type;
@Constraints.Required
@Formats.NonEmpty
@Column(nullable=false)
public String document;
}
Run Code Online (Sandbox Code Playgroud)
我在我的控制器中使用它来匹配枚举:
DynamicForm form = form().bindFromRequest();
// ...
Document doc = new Document();
doc.type = DocumentTypes.valueOf(form.field("type").value());
doc.save(); …Run Code Online (Sandbox Code Playgroud) 我在Play Framework 2中使用Ebean,有时会出现类似的OptimisticLockException:
play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[OptimisticLockException: Data has changed. updated [0] rows sql[update manager set modification_time=?, session_id=?, expiration_date=? where id=? and rating=? and creation_time=? and modification_time=? and name=? and surname=? and login=? and password_hash=? and email=? and session_id=? and expiration_date=?] bind[null]]]
Run Code Online (Sandbox Code Playgroud)
当少数演员开始访问数据库时会发生这种情况
那么,Manager类是:
public class Manager extends Model {
@Getter @Setter
Long id;
@Getter @Setter
private String name;
@Getter @Setter
private String surname;
@Column(unique = true)
@Getter @Setter
private String login;
@Getter @Setter
private String passwordHash;
@Getter @Setter
private …Run Code Online (Sandbox Code Playgroud) 我有一个想要修改db中的行的play framework 2.0.4应用程序.
我需要将db中的'less'消息更新为"打开"状态(读取消息)我就像下面这样做了
String sql = " UPDATE message SET opened = true, opened_date = now() "
+" WHERE id_profile_to = :id1 AND id_profile_from = :id2 AND opened IS NOT true";
SqlUpdate update = Ebean.createSqlUpdate(sql);
update.setParameter("id1", myProfileId);
update.setParameter("id2", conversationProfileId);
int modifiedCount = update.execute();
Run Code Online (Sandbox Code Playgroud)
我修改了postgresql来记录所有查询.
modifiedCount是修改行的实际数量 - 但查询是在事务中.在db中完成查询之后会有ROLLBACK - 因此不会进行UPDATE.我试图将db更改为H2 - 结果相同.
这是来自postgres审核日志的查询
2012-12-18 00:21:17 CET : S_1: BEGIN
2012-12-18 00:21:17 CET : <unnamed>: UPDATE message SET opened = true, opened_date = now() WHERE id_profile_to = $1 AND id_profile_from = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Ebean在Play中创建以下类:
public class BaseModel extends Model {
@Column(name = "created_at")
public Date createdAt;
@Column(name = "updated_at")
public Date updatedAt;
@PrePersist
public void createdAt() {
this.createdAt = this.updatedAt = new Date();
}
@PreUpdate
public void updatedAt() {
this.updatedAt = new Date();
}
}
Run Code Online (Sandbox Code Playgroud)
它编译没问题,但是当我用我的实体bean子类化这个类时(所以每个都会插入时间戳进行创建和更新)我在尝试点击应用程序时遇到以下异常:
play.api.UnexpectedException: Unexpected exception[PersistenceException: Error with [models.InventoryItem] I believe it is not enhanced but it's superClass [class models.BaseModel] is? (You are not allowed to mix enhancement in a single inheritance hierarchy)]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:150) ~[play_2.10.jar:2.1.4]
at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:114) ~[play_2.10.jar:2.1.4]
at …Run Code Online (Sandbox Code Playgroud) 运行Play Framework 2.2.x(Java)项目时出现以下错误:
Configuration error
Cannot register class [models.SomeClass] in Ebean server
Run Code Online (Sandbox Code Playgroud)
浏览器中显示的错误消息将我指向我的行ebean.default="models.*",application.conf控制台告诉我我的java.lang.VerifyError: Bad type on operand stack一个方法.对于这种情况发生的方法没有什么特别之处,现在已经发生了一些方法.
我发现使用静态方法可以避免错误:即替换someObject.doJob()为SomeClass.doJob(someObject).我已经使用过这个hack,它可以工作,但是我不是很高兴我的所有方法都应该是静态的.
有没有人遇到同样的问题,并找到了解决方法(不使方法静态)?
最近升级到Play 2.4,我还在学习所有的小怪癖等等.我正试图让我的索引页面正常工作而且我很难接受它,而且我知道它很小,我很想念.这是错误
CreationException: Unable to create injector, see the following errors:
1) Error in custom provider, Configuration error: Configuration error[Cannot connect to database [default]]
while locating play.api.db.DBApiProvider
while locating play.api.db.DBApi
for parameter 0 at play.db.DefaultDBApi.<init>(DefaultDBApi.java:28)
at play.db.DefaultDBApi.class(DefaultDBApi.java:28)
while locating play.db.DefaultDBApi
while locating play.db.DBApi
for field at play.db.DBModule$NamedDatabaseProvider.dbApi(DBModule.java:61)
while locating play.db.DBModule$NamedDatabaseProvider
at com.google.inject.util.Providers$GuicifiedProviderWithDependencies.initialize(Providers.java:149)
at play.db.DBModule.bindings(DBModule.java:40):
Run Code Online (Sandbox Code Playgroud)
和我的配置信息(是的,mysql的端口号是正确的)
application.conf
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql:localhost:33060/coffee_bean"
db.default.username=
db.default.password=""
ebean.default = ["models.*"]
Run Code Online (Sandbox Code Playgroud)
主控制器
public Result index() {
ObjectNode response = Json.newObject();
Configuration config = Play.application().configuration();
response.put(config.getString("coffee.bean.message.key"),config.getString("coffee.bean.success.message"));
response.put(config.getString("version"), config.getString("coffee.bean.version"));
return …Run Code Online (Sandbox Code Playgroud) 我不想在application.conf文件中定义默认数据库配置.相反,我想以编程方式构造默认的EbeanServer并将其自己注入DAO.
我遇到的问题是,如果我为EbeanServer提供程序创建一个guice绑定而没有在application.conf文件中定义任何配置,则播放错误,说它无法找到默认配置.
这是我的代码:
public class EbeanServerProvider implements Provider<EbeanServer> {
final Logger.ALogger log = Logger.of(this.getClass());
@Override
public EbeanServer get() {
ServerConfig serverConfig = new ServerConfig();
DataSourceConfig dataSourceConfig = new DataSourceConfig();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Config dbConfig = ConfigFactory.load(classLoader,"env/default.conf");
/* Read the config files */
final String DB_DRIVER = dbConfig.getString("db.default.driver");
final String DB_URL = dbConfig.getString("db.default.url");
final String DB_USERNAME = dbConfig.getString("db.default.username");
final String DB_PASSWORD = dbConfig.getString("db.default.password");
log.debug("{}",DB_DRIVER);
log.debug("{}",DB_URL);
log.debug("{}",DB_USERNAME);
log.debug("{}",DB_PASSWORD);
/* Load the database driver */
dataSourceConfig.setDriver(DB_DRIVER);
try{
Class.forName(DB_DRIVER).newInstance(); …Run Code Online (Sandbox Code Playgroud) 我在尝试搜索列表中的特定Java对象时遇到了问题.
实际上我得到了我想为另一个Java类工作的搜索功能.现在我尝试了另一个但不是返回结果列表我得到一个NullPointerException.
这就是我的功能:
public static List<Customer> searchByEverything(String keyword){
List<Customer> customerList = find.all(); //Using java Ebean
System.out.println(keyword); //Check if keyword is not empty
System.out.println(customerList); //Check if list is not empty
Predicate<Customer> customerPredicate = u -> u.name.toLowerCase().contains(keyword.toLowerCase());
try{
return customerList.stream().filter(customerPredicate).collect(Collectors.toList());
}catch (Exception e){
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
这是来自Exception e的strack trace:
java.lang.NullPointerException
at models.Customer.lambda$searchByEverything$2(Customer.java:174)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:174)
at java.util.ArrayList$Itr.forEachRemaining(ArrayList.java:891)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at models.Customer.searchByEverything(Customer.java:179)
at controllers.Application.searchCustomers(Application.java:262)
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$17$$anonfun$apply$17.apply(Routes.scala:479)
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$17$$anonfun$apply$17.apply(Routes.scala:479)
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 …Run Code Online (Sandbox Code Playgroud) ebean ×10
java ×4
enums ×1
guice ×1
lambda ×1
list ×1
postgresql ×1
sql ×1
sql-update ×1
subclassing ×1
transactions ×1