Java jpa 查询包含空字段的 WordPress 数据库时出错

标签 java mysql wordpress jpa eclipselink

我有一个java应用程序需要访问wordpress数据库,因为我无法使用wordpress xml-rpc来完成我需要的所有操作。我需要在 WordPress 中访问的主表是 wp_posts 表。我正在使用 eclipseLink 2.6 和 mysql 5.1.34

以下是我的 wp_posts 实体

@Entity
@Table(name="wp_posts")
public class WpPost implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name="ID")
private Integer id;

@Column(name="comment_count")
private Integer commentCount;

@Column(name="comment_status")
private String commentStatus;

@Column(name="guid")
private String guid;

@Column(name="menu_order")
private Integer menuOrder;

@Column(name="ping_status")
private String pingStatus;

More vars + getters and setters etc.....

我像这样运行查询

public final static String SELECT_ALL_WP_POST_ENTITIES_SQL = "SELECT o FROM WpPost AS o";

public List<WpPost> getAllEntities() {

    final EntityManager entityManager = DaoUtilities
            .getEntityManagerFactory().createEntityManager();
    final List<WpPost> posts;
    try {
        posts = (List<WpPost>) entityManager.createQuery(
                SELECT_ALL_WP_POST_ENTITIES_SQL).getResultList();
    } finally {
        entityManager.close();
    }

    return posts;

}

我收到以下错误

[EL Info]: server: 2015-03-31 10:10:59.234--ServerSession(472654579)--Detected server platform: org.eclipse.persistence.platform.server.NoServerPlatform.
[EL Info]: server: 2015-03-31 10:10:59.682--ServerSession(472654579)--Detected server platform: org.eclipse.persistence.platform.server.NoServerPlatform.
[EL Info]: 2015-03-31 10:11:00.296--ServerSession(472654579)--EclipseLink, version: Eclipse Persistence Services - 2.6.0.v20150309-bf26070
[EL Info]: connection: 2015-03-31 10:11:00.752--ServerSession(472654579)--/file:/home/alex/Dropbox/eclipseProjects/docoCrawler/wordpressDB/target/classes/_DefaultPersistence login successful
[EL Warning]: sql: 2015-03-31 10:11:00.818--ServerSession(472654579)--java.sql.SQLException: Value '390open;http://127.0.0.1/wordpress/?post_type=optionsframework&p=390open
[EL Warning]: 2015-03-31 10:11:00.82--UnitOfWork(91430202)--Exception     [EclipseLink-4002] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Value '390open;http://127.0.0.1/wordpress/?post_type=optionsframework&p=390open
Error Code: 0
Call: SELECT ID, comment_count, comment_status, guid, menu_order,  ping_status, pinged, post_author, post_content, post_content_filtered, post_date, post_date_gmt, post_excerpt, post_mime_type, post_modified, post_modified_gmt, post_name, post_parent, post_password, post_status, post_title, post_type, to_ping FROM wp_posts
Query: ReadAllQuery(referenceClass=WpPost sql="SELECT ID,  comment_count, comment_status, guid, menu_order, ping_status, pinged, post_author, post_content, post_content_filtered, post_date, post_date_gmt, post_excerpt, post_mime_type, post_modified, post_modified_gmt, post_name, post_parent, post_password, post_status, post_title, post_type, to_ping FROM wp_posts")
Exception in thread "main" javax.persistence.PersistenceException:     Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Value '390open;http://127.0.0.1/wordpress/?post_type=optionsframework&p=390open
Error Code: 0
Call: SELECT ID, comment_count, comment_status, guid, menu_order, ping_status, pinged, post_author, post_content, post_content_filtered, post_date, post_date_gmt, post_excerpt, post_mime_type, post_modified, post_modified_gmt, post_name, post_parent, post_password, post_status, post_title, post_type, to_ping FROM wp_posts
Query: ReadAllQuery(referenceClass=WpPost sql="SELECT ID, comment_count, comment_status, guid, menu_order, ping_status, pinged, post_author, post_content, post_content_filtered, post_date, post_date_gmt, post_excerpt, post_mime_type, post_modified, post_modified_gmt, post_name, post_parent, post_password, post_status, post_title, post_type, to_ping FROM wp_posts")
at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:382)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)
at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:473)
at dao.PostDao.getAllEntities(PostDao.java:43)
at dao.PostDao.main(PostDao.java:33)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Value '390open;http://127.0.0.1/wordpress/?post_type=optionsframework&p=390open
Error Code: 0
Call: SELECT ID, comment_count, comment_status, guid, menu_order, ping_status, pinged, post_author, post_content, post_content_filtered, post_date, post_date_gmt, post_excerpt, post_mime_type, post_modified, post_modified_gmt, post_name, post_parent, post_password, post_status, post_title, post_type, to_ping FROM wp_posts
Query: ReadAllQuery(referenceClass=WpPost sql="SELECT ID, comment_count, comment_status, guid, menu_order, ping_status, pinged, post_author, post_content, post_content_filtered, post_date, post_date_gmt, post_excerpt, post_mime_type, post_modified, post_modified_gmt, post_name, post_parent, post_password, post_status, post_title, post_type, to_ping FROM wp_posts")
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.getObject(DatabaseAccessor.java:1331)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.fetchRow(DatabaseAccessor.java:1077)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.processResultSet(DatabaseAccessor.java:770)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:657)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:560)
at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2055)
at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:570)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:299)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:694)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2740)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2693)
at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:541)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1173)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:904)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1132)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:442)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1220)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1857)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1839)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258)
... 3 more
Caused by: java.sql.SQLException: Value   '390open;http://127.0.0.1/wordpress/?post_type=optionsframework&p=390open
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:870)
at com.mysql.jdbc.ResultSetRow.getTimestampFast(ResultSetRow.java:928)
at com.mysql.jdbc.BufferRow.getTimestampFast(BufferRow.java:555)
at com.mysql.jdbc.ResultSetImpl.getTimestampInternal(ResultSetImpl.java:5943)
at com.mysql.jdbc.ResultSetImpl.getTimestamp(ResultSetImpl.java:5609)
at com.mysql.jdbc.ResultSetImpl.getObject(ResultSetImpl.java:4582)
at org.eclipse.persistence.internal.databaseaccess.DatabasePlatform.getObjectFromResultSet(DatabasePlatform.java:1392)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.getObject(DatabaseAccessor.java:1302)
... 26 more

在 wp_posts 表上,所有列都选择了“not null”,但并非全部都给出了默认值。从我的错误消息中我可以看到行

java.sql.SQLException: Value '390open;http://127.0.0.1/wordpress/?post_type=optionsframework&p=390open 

它正在从数据库获取第一行,其值为“39, 0, open, http://127.0.0.1/wordpress/?post_type=optionsframework&p=39” , 0, open' 映射到 'SELECT ID, comment_count, comment_status, guid, menu_order, ping_status'。然而,查询“pinged”中的下一个字段在数据库中没有值,这就是它失败的地方。如果我向该字段添加一个值,那么新值将添加到 sql 异常错误消息中,但稍后我会在几个字段中遇到问题,其中有更多字段没有值。如果我向所有字段添加值,则查询将运行。

我不想开始修改 WordPress 数据库。如何处理数据库中不为空但没有值的空字段?

更新:我认为是日期字段导致了问题。 WordPress 数据库中日期的默认值为 0000-00-00 00:00:00。如果数据库中有一行包含所有字段的值,那么像 0000-00-00 00:00:00 这样的日期不会引起问题。但是,如果我有一行的字段没有值,而其他日期字段的值类似于 0000-00-00 00:00:00,那么我会收到上述错误。或者,如果所有日期字段都有正确的日期值,则没有值的字段不会导致任何问题。因此,看来只有当我的行的日期字段值为 0000-00-00 00:00:00 并且我还有其他没有值的字段时,才会出现此问题。我所有的日期字段都用 @Temporal(TemporalType.TIMESTAMP) 标记,如下所示:

@Temporal(TemporalType.TIMESTAMP)
@Column(name="post_modified")
private Date postModified;

最佳答案

所以我已经解决了这个问题。我需要在 persistence.xml 中配置我的连接,通过添加 ZeroDateTimeBehavior=convertToNull 来处理零日期和时间,如下所示:

<property name="javax.persistence.jdbc.url"
            value="jdbc:mysql://localhost:3306/wordpress_4.1?zeroDateTimeBehavior=convertToNull" />

这解决了我的问题

关于Java jpa 查询包含空字段的 WordPress 数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29358330/

相关文章:

java - 如何在 int 数组中找到两个不同的值?

mysql - 在 MySQL 中添加外键失败 - errno 150 - 使用 mysqldump

css - 根据页面更改导航项颜色

php - 使用OnePress时,我可以更改toppage的字体吗?

java - 有没有办法使用 jaxb 添加带有 xml 的内联模式

java - Oracle Advanced Queue - 消耗率的性能问题

java - 如何创建随机分布在阵列上的图像的 2D 阵列?

php - 为什么对已经存在的标准函数使用自定义 sql 函数?

mysql - MySQL 查询中的奇怪返回

javascript - 使用 NG-CLICK 更改单击元素的类