我有一个需要访问wordpress数据库的java应用程序,因为我不能完全使用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' 。但是,查询“pingged”中的下一个字段在数据库中没有任何值,这就是它失败的地方。如果我在这个字段中添加一个值,那么新的值会被添加到sql异常错误消息中,但是我会在以后有几个字段没有值的情况下遇到问题。如果我向所有字段添加值,则查询将运行。
我不想开始修改wordpress数据库。如何处理数据库中非空但没有值的空字段?
更新:我认为是造成问题的日期字段。 wordpress数据库中日期的默认值为0000-00-00 00:00:00。如果我在数据库中有一行具有所有字段的值,那么像00:00:00这样的日期不会导致问题。但是,如果我有一行没有值的字段,其他日期字段的值如0002 00:00:00那么我得到上述错误。或者,如果所有日期字段都具有正确的日期值,则没有值的字段不会导致任何问题。所以看起来这个问题只是在我有00:00:00的行作为日期字段的值而且我还有其他没有值的字段时引起的。我的所有日期字段都标有@Temporal(TemporalType.TIMESTAMP),如下所示:
@Temporal(TemporalType.TIMESTAMP)
@Column(name="post_modified")
private Date postModified;
答案 0 :(得分:0)
所以我解决了这个问题。我需要在persistence.xml中配置我的连接,通过添加zeroDateTimeBehavior = convertToNull来处理零日期和时间,如下所示:
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/wordpress_4.1?zeroDateTimeBehavior=convertToNull" />
这解决了我的问题