English 中文(简体)
scala hibernate/jpa - 忽略自动生成的位图$0 映射
原标题:scala hibernate/jpa - ignore autogenerated bitmap$init$0 mapping

试图使用冬眠/ jpa 与 scala 。 遇到有趣的问题 。

这是我的实体定义。

@Entity
class Product(n: String, d: Double) extends EntityBase {

  def this() = this("", 0)
  def this(n: String) = this(n, 0)

  var name: String = n
  var price: Double = d

  @ManyToOne
  @JoinColumn(name="orderId")
  var order: Order = _

  override def toString = "Product: " + id + " " + name
}

当我进行冬眠查询时,我得到以下例外:

[SQLGrammarException: ERROR: column this_.bitmap$init$0 does not exist Position: 29]

显然 jpa 默认地创建 scala 自动生成的 位图字段 $0 的映射 。 我不知道是什么导致 scala 生成它 。 但是无论有什么办法可以让 jpa 忽略它? 或者把它从冬眠映射中移除 。 或者其他的 。

问题回答

我不知道自动生成的字段来自何方, 但你可以试着简化您的课程, 以减少字段 :

@Entity
class Product(var name: String = "", var price: Double = 0) extends EntityBase {

  @ManyToOne
  @JoinColumn(name="orderId")
  var order: Order = _

  override def toString = "Product: " + id + " " + name
}

发现了一些有趣的事情 关于这个问题。

我的应用程序中有这个 < code> 模式 < /code> 类 :

@Entity
@Table(name="users")
class User extends Model{
  @Id
  @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="users_id_seq")
  @Column(name="id", updatable=false, nullable=false)
  val id:Long = 0L

  @BeanProperty var name:String = _
  @BeanProperty var email:String = _ 

时 时

此类编译为名为 User 的 java 类, 其定义如下:

@Entity
@Table(name="users")
@ScalaSignature(bytes=long array of bytes)
public class User extends Model
   implements ScalaObject, EntityBean
{

   @Id
   @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="users_id_seq")
   @Column(name="id", updatable=false, nullable=false)
   private final long id;
   private String name;
   private String email;
   private volatile int bitmap$init$0;
   private static String _EBEAN_MARKER = "models.User";

   ...

   public long id()
  {
    if ((_ebean_get_bitmap$init$0() & 0x1) != 0)
    {
        _ebean_get_id(); return _ebean_get_id(); 
   时 时 
   throw new 
         UninitializedFieldError("Uninitialized field: User.scala: 17".toString());
    时 时

   public String name()
   {
   if ((_ebean_get_bitmap$init$0() & 0x2) != 0)
   {
     _ebean_get_name(); return _ebean_get_name();
   时 时 throw new 
           UninitializedFieldError("Uninitialized field: User.scala: 19".toString()); 
   时 时 

   public void name_$eq(String paramString) { 
     _ebean_set_name(paramString); 
      _ebean_set_bitmap$init$0(_ebean_get_bitmap$init$0() | 0x2);
   时 时

  ....

The bitmap$init$0 actually comes from a class enhancement, and I thought that the responsible was the Ebean library, which I m currently using. But after reading your post I went to investigate if JPA itself was doing some sort of bytecode enhancement on this object. In order to check this out, I created a copy project, but in Java. The generated code for the User class happens to have no bitmap$init$0 field, as follow:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="users")
public class User
{

  @Id
  @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="users_id_seq")
  @Column(name="id", updatable=false, nullable=false)
  private Long id;
  private String name;
  private String email;

 public void setId(Long id)
 {
    this.id = id;
 时 时

 public Long getId() {
   return this.id;
 时 时

public String getName() {
   return this.name;
 时 时
 public void setName(String name) {
   this.name = name;
 时 时
 public String getEmail() {
    return this.email;
 时 时
 public void setEmail(String email) {
    this.email = email;
  时 时
时 时

所有这些麻烦导致我来到这个 rel=“nofollow norefererr”> post ,我非常肯定地同意。似乎将JPA与Scala Ebean/Hibertnate结合是一种真正的痛苦。我仍然不明白, Ebean 或其他东西是否将这个 bittmap$ini0 字段注入了类位元代码 。

似乎你可以试图绕过这种行为,将注解()与生成的爪哇字节码联系起来,首先为 java 编译代码,然后到 Scala 编译代码,类似于< a href=> https://stackoverflow.com/ questions/11493543/compiliting-java-annotions-with-sbt” 。但我真的不认为这很值得!





相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签