2015-03-30 75 views
0

我有一个需要访问wordpress数据库的java应用程序,因为我无法做所有我需要使用wordpress xml-rpc。我需要在wordpress中访问的主表是wp_posts表。我使用的EclipseLink 2.6和MySQL 5.1.34Java jpa错误查询包含空字段的wordpress数据库

以下是我的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,越来越开放的第一行从数据库,http://127.0.0.1/wordpress/?post_type=optionsframework&p=39,0看,开放“映射到” SELECT ID, comment_count,comment_status,guid,menu_order,ping_status'。然而,查询'pinged'中的下一个字段在数据库中没有任何价值,这是失败的地方。如果我给这个字段添加一个值,那么新的值会被添加到sql异常错误消息中,但是如果有更多字段没有值,我会在几个字段中遇到问题。如果我将值添加到所有字段,则查询运行。

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

UPDATE:我认为这是导致问题的日期字段。 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; 
+1

是“ping通”一个TIMESTAMP/DATETIME列? (“pingStatus”当然不在你的课堂上)。对于基于异常的那种类型的列似乎有问题 - 哪一列是时间戳? – 2015-03-31 07:33:05

回答

0

所以我有固定的问题。我需要通过增加zeroDateTimeBehavior = convertToNull像这样来配置我在我的persistence.xml处理零日期和时间连接:

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

这已固定我的问题