0

I'm trying to use entities with a MySQL ndbcluster table. This table type doesn't allow foreign keys, but up until now it hasn't been a problem with my entities.

However, I have run into a bit of a problem, when I try to load an entity using the EntityManager's createNativeQuery method. I need to use this method due to my inability to do this: How to make a CriteriaBuilder join with a custom "on" condition?

My MySQL table looks like this:

CREATE TABLE `category` (
    `id` SMALLINT(6) NOT NULL AUTO_INCREMENT,
    `category_id` SMALLINT(6) NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    INDEX `category_id` (`category_id`)
)
COLLATE='utf8_general_ci'
ENGINE=ndbcluster
ROW_FORMAT=DEFAULT

If I change the table engine to innodb, and add foreign keys, the createNativeQuery method works fine.

My entity class looks like this:

@Entity
@Table(name = "category")
public class Category implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Short id;
    @OneToMany(mappedBy = "categoryId")
    private List<Category> categoryList;
    @JoinColumn(name = "category_id", referencedColumnName = "id")
    @ManyToOne
    private Category categoryId;

    public Category() {
    }

    // getters and setters
}

Even without foreign keys, this entity works fine when I use the CriteriaBuilder for a query, but unfortunately not everything is possible with the CriteriaBuilder.

I get an error when I call getResultList on a Query object created with createNativeQuery. I don't know if this is a bug, or if something should be added to my entity class to make this work.

The error says:

Exception [EclipseLink-6044] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.QueryException
Exception Description: The primary key read from the row [ArrayRecord(
 => 2519
 => 2463
 => Tools)] during the execution of the query was detected to be null.  Primary keys must not contain null.
Query: ReadAllQuery(referenceClass=Category sql="select * from `category`")
at org.eclipse.persistence.exceptions.QueryException.nullPrimaryKeyInBuildingObject(QueryException.java:895)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:584)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildObject(ObjectBuilder.java:560)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildObject(ObjectLevelReadQuery.java:717)
at org.eclipse.persistence.queries.ReadAllQuery.registerResultInUnitOfWork(ReadAllQuery.java:769)
...

My table contains 1 row, where id=1 and category_id=null, so there are no primary keys with a null-value, despite what the error says. If I remove that row or set category_id=1, I don't get an error.

Need help, please.

4

2 回答 2

1

通过从 EclipseLink (JPA 2.0) 切换到 OpenJPA (JPA 2.0) 来管理它。似乎 EclipseLink 2.3.2 和/或 GlassFish 3.1.2.2 中的某个地方存在错误。

我在我的另一个项目中使用了 EclipseLink (JPA 2.0),使用稍微不同的版本 Netbeans + GlassFish 3.1.1,我在实体类上使用 createNativeQuery 来获取非关系 myisam 表。这从来没有引起任何问题。确实应该是bug。

但是问题解决了。再见,再见 EclipseLink,你好 OpenJPA。

于 2013-04-13T21:10:11.053 回答
0

问题是区分大小写。在 MySQL 中,除非您引用它,否则您的列“id”将在数据库中定义为“ID”。如果您将映射切换为大写,它应该可以解决问题(即“ID”)。

您还可以引用列名(“'id'”)

或设置持久性单元属性,

“eclipselink.jpa.uppercase-column-names”=“真”

于 2013-04-15T13:25:44.003 回答