【问题标题】:Use save() method in Play! with inheritance (JPA)在 Play 中使用 save() 方法!继承(JPA)
【发布时间】:2015-02-10 09:39:56
【问题描述】:

我有我的超级抽象类:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class User {

    @Id
    public int id; 
    public String name; 
}

另外两个类扩展了用户:

客户

@Entity
@Table(name = "customers")
public class Customer extends User{

    @Id
    public int id; 
    public String role; 

    public Customer(String role){
        super(); 
        this.role = role; 
    }
}

卖家

@Entity
@Table(name = "sellers")
public class Seller extends User{

    @Id
    @GeneratedValue
    public int id; 
    public String role; // Seller

    public Seller(String role){
        super(); 
        this.role = role; 
    }
 }

我希望能够在游戏中使用 save() 方法,所以我写了这个:

public static Result saveCustomer(){
        Customer customer = Form.form(Customer.class).bindFromRequest().get();
        customer.save();        
        return ok(); 
    }

但是,save() 没有定义。

解决这个问题的方法是什么?

【问题讨论】:

    标签: java jpa playframework playframework-2.2 playframework-2.3


    【解决方案1】:

    实际上...要在 play 2.x 中获得 entityManager,您有一个 Helper。

    JPA.em().persist(object);
    

    您可以在此link 中查看更多信息。

    【讨论】:

    • 请...如果它帮助您将其标记为正确,那么我们关闭它:) @Mornor
    【解决方案2】:

    save() 方法是属于Play 1.xGenericModel 的一部分。因为,你使用Play 2.x,你应该使用JPA 对象和entityManager.persist() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 2011-08-20
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多