I have a java application that needs to access a wordpress database as I can not do all I need to using wordpress xml-rpc. The main table I need to access in wordpress is the wp_posts table. I am using eclipseLink 2.6 and mysql 5.1.34
The following is my wp_posts entity
@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.....
And I run my query like so
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;
}
I get the following error
[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://ift.tt/19sJaCH
[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://ift.tt/19sJaCH
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://ift.tt/19sJaCH
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://ift.tt/19sJaCH
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://ift.tt/19sJaCH
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
On the wp_posts table all columns have 'not null' selected however they are not all given default values. From my error message I can see from the line
java.sql.SQLException: Value '390open;http://ift.tt/19sJaCH
that it is getting the first row from the database with the values '39, 0, open, http://ift.tt/1CF08ck, 0, open' that map to 'SELECT ID, comment_count, comment_status, guid, menu_order, ping_status'. However the next field in the query 'pinged' has no value in the database and this is where it fails. If I add a value to this field then the new value is added to the sql exception error message but I run into problems a few fields later where there are more fields that have no values. If I add values to all fields then the query runs.
I dont want to start modifying the wordpress database. How can I handle empty fields in the database that are not null but have no value?
UPDATE: I think it is the date fields that are causing the problems. The default value for a date in the wordpress database is 0000-00-00 00:00:00. If I have a row in the database that has values for all fields then the dates like 0000-00-00 00:00:00 don't cause a problem. However if I have a row that has fields with no value and other date fields have values like 0000-00-00 00:00:00 then I get the above error. Alternatively if all date fields have proper date values then fields with no value don't cause any problems. So it appears that the problem is only caused when I have rows with 0000-00-00 00:00:00 as the value for date fields and I also have other fields with no value. All my date fields are marked with @Temporal(TemporalType.TIMESTAMP) like so:
@Temporal(TemporalType.TIMESTAMP)
@Column(name="post_modified")
private Date postModified;
Aucun commentaire:
Enregistrer un commentaire