我正在尝试使用多对多方法产品与商店 Product.java具有多对多关系
package models;
@Entity
public class Product extends Model {
@Id
@SequenceGenerator(name="product_gen", sequenceName="product_id_seq", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="product_gen")
@Column(name="id")
public Long id;
@Required
public String name;
@Required
public Float price;
@ManyToMany(cascade = CascadeType.ALL)
public List<Shop> shops = new ArrayList<Shop>();
public Product(String name, float price) {
this.name = name;
this.price = price;
}
public static List<Product> all(){
return find.all();
}
public static Model.Finder<Long, Product> find = new Model.Finder(Long.class, Product.class);
public static Product create(String name,float price) {
Product product = new Product(name, price); …Run Code Online (Sandbox Code Playgroud) 在检索对象列表时,我想根据另一个只有ID的表来过滤结果.对象在ORM模型中没有链接,而只是包含一个UUID.
即:
@Entity
class A {
@Id
private UUID id;
private UUID refB; // links to B
}
@Entity
class B {
@Id
private UUID id;
private boolean visible;
}
Run Code Online (Sandbox Code Playgroud)
我想检索B.hidden为假或其中B不存在的所有A.
在SQL中我会做类似的事情
SELECT t0.* FROM a_table t0 LEFT JOIN b_table t1 ON (t0.ref_b = t1.id)
WHERE t1.hidden IS NULL OR t1.hidden = 0;
Run Code Online (Sandbox Code Playgroud)
我不仅仅使用RawSql的原因是我找不到任何方法在select中使用通配符,因此所有属性都必须在select中进行维护和手动添加.
我也试过了
List<A> listA = Ebean.find(A.class).where()
.join("LEFT JOIN b_table t1 ON (t0.ref_b = t1.id)")
.where().in("t1.hidden", "0", "NULL");
Run Code Online (Sandbox Code Playgroud)
但后来我得到一个错误,因为在"LEFT JOIN"之前放置了WHERE.
我认为"正确"的方式是将"私有UUID refB"替换为"私有B refB".但这样做可以更容易规避某些安全措施.
这是可能的还是我必须在RawSql中添加所有属性?
我需要一些帮助.我有两个关于onetomany关系的课程:
@Entity
public class Parent extends Model{
@Id
public Long id;
@OneToMany(fetch = FeatchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
public List<Child> children;
}
Run Code Online (Sandbox Code Playgroud)
和
@Entity
public class Child extends Model{
@Id
public Long id;
}
Run Code Online (Sandbox Code Playgroud)
所以当我调用remove()时,Child实体不会从DB中删除.
Parent parent = Parent.find.byId(id);
parent.children.remove(parent.children.get(0));
parent.save();
Run Code Online (Sandbox Code Playgroud)
下次我发现.byId - 所有的孩子都在那里,就像他们从未被删除一样:(
在内存数据库中播放2.0.4.
如果需要任何其他信息,请告诉我.
我需要一些初始数据(从csv文件)到数据库.我正在使用Ebean和Play!框架.我读了文档.它说使用YAML文件存储数据并调用Ebean.save().这是在测试中完成的.
我的问题:
我应该在哪里插入数据?(测试可能不是理想的地方,因为这些数据应该用于生产)
我可以编写自己的代码来从现有的csv文件中获取数据,而不是使用YAML文件吗?
任何建议或文档链接将不胜感激.
谢谢!
我有一个带有字段 id 、 name 、 price 等的类产品,...
我只想从表中获取名称..
我目前正在使用此查询
String sql = "select name from product where price = 100";
SqlQuery sqlQuery = Ebean.createSqlQuery(sql);
List<SqlRow> list = sqlQuery.findList();
Run Code Online (Sandbox Code Playgroud)
使用 List 查找但仅获取名称的警报方式是什么
List<product> list = product.find.where("price = 100").select("name").findList();
Run Code Online (Sandbox Code Playgroud)
我认为以下查询效率不高,因为它获取所有数据并返回我们对其进行过滤的情况
List<String> list = new ArrayList<String>();
for(product p: product.find.select("name").findList())
{
list.add(p.name);
}
return list;
Run Code Online (Sandbox Code Playgroud) 当我有:
public class User extends Model {
@Id
public Long id;
@Constraints.Required
@Formats.NonEmpty
public String username;
Run Code Online (Sandbox Code Playgroud)
public String firstName; public String lastName;
我可以做User.find.byUsername("myusername")或者User.find.byFirstNameAndLastName...我必须在User类中定义方法吗?
谢谢!
我在从现有数据库表(SQL Server 2008 R2)返回数据时遇到问题.我可以成功验证并连接到数据库,我创建了一个模型,我试图映射到特定的表,然后作为测试尝试返回行计数,行计数总是返回0.我想也许我不明白Play/Ebean数据库连接如何工作.目前我有以下内容:
型号 - Data.java:
package models;
import javax.persistence.*;
import play.db.ebean.*;
@Entity
@Table(name="someTable")
public class Data extends Model {
private static final long serialVersionUID = 1L;
@Id
public int someKey;
public String someCol;
public static Finder<Integer,Data> find = new Finder<Integer,Data>( Integer.class, Data.class );
}
Run Code Online (Sandbox Code Playgroud)
控制器 - Index.java
package controllers;
import java.util.*;
import models.Data;
import play.mvc.*;
public class Index extends Controller {
static int rowCount = Data.find.getMaxRows();
public static Result index() {
Result res = ok(rowCount);
return res;
} …Run Code Online (Sandbox Code Playgroud) 现在我有
@Entity
public class Argument extends Model
{
@Id
public Long id;
@Required @NotEmpty @Size(max = 140)
public String summary;
@SuppressWarnings("unchecked")
public static Finder<Long, Argument> find = new Finder(Long.class, Argument.class);
...
}
Run Code Online (Sandbox Code Playgroud)
和
@Entity
public class Relation extends Model
{
@Id
public Long id;
@Required @ManyToOne @NotNull @JsonManagedReference
public Argument from;
@ManyToOne @JsonManagedReference
public Argument toArgument;
@ManyToOne @JsonManagedReference
public Relation toRelation;
@Required @NotNull
public Integer type;
...
}
Run Code Online (Sandbox Code Playgroud)
基本上,Relation将两个参数(或参数和另一个关系)链接在一起.这是两个班级之间的单向关系.然而我明白了
[RuntimeException: java.lang.IllegalArgumentException: Infinite recursion
(StackOverflowError) (through reference chain: models.Argument["relations"]-> …Run Code Online (Sandbox Code Playgroud) 我在Play Framework 2.2.3中有以下文件
控制器:
public class Comment extends Controller
{
public Result create(UUID id)
{
models.blog.Blog blog = models.blog.Blog.finder.byId(id);
Result result;
if(blog == null)
{
result = notFound(main.render("404", error404.render()));
}
else
{
Form<models.blog.Comment> commentForm = Form.form(models.blog.Comment.class);
commentForm = commentForm.bindFromRequest();
if(commentForm.hasErrors())
{
result = badRequest(Json.toJson(commentForm));
}
else
{
models.blog.Comment comment = commentForm.get();
comment.setId(UUID.randomUUID());
comment.setTimeCreated(new Date());
comment.setBlogId(blog.getId());
comment.save();
result = ok(Json.toJson(comment));
}
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
还有两个型号
@Entity
@Table(name="blog")
public class Blog extends Model
{
private static final SimpleDateFormat MONTH_LITERAL = …Run Code Online (Sandbox Code Playgroud)