JPA2: create-or-extend-tables does not extend existing table with new column
I use JPA2
, EclipseLink
. I am trying to add a new column to an existing database and table. I added a new field to the POJO class MyTable. Despite the properties create-or-extend-tables
, the new column does not stack.
Here is my file persistence.xml
.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="idpersistance">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.id.service.db.pojo.AppTable</class>
<class>com.id.service.db.pojo.MyTable</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"></property>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:2705/mydb"></property>
<property name="javax.persistence.jdbc.user" value="user"></property>
<property name="javax.persistence.jdbc.password" value="password"></property>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.id-validation" value="NULL"></property>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="javax.persistence.lock.timeout" value="1000"/>
<property name="eclipselink.target-database" value="MySQL"/>
</properties>
</persistence-unit>
</persistence>
I'm not sure why the new column is not being added. I am returning after an error:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: null, message from server: "Unknown column 'newcol' in 'field list'"
Error Code: 1054
The file jar
I'm using:
eclipselink.jar (2.5.2)
javax.persistence_2.1.0.v201304241213.jar
Please let me know if persistence.xml
something is missing from my file .
Any help is appreciated.
Update:
I debugged the code EclipseLink
and found that it doesn't try to generate new columns, it throws an error on a method extendDefaultTables()
in the class EntityManagerFactoryProvider
and says the table already exists and returns from that method.
There is a field named in the incrementCallCount()
class DatasourceAccessor
field useExternalConnectionPooling
if I set this field to true manually. Then new columns get generated. Due to the field false, it EclipseLink
does not start. I'm not sure if this is the correct field.
source to share
Set eclipselink.logging.level.sql
to FINE
, 1) by editing config files, or 2) programmatically:
Map<String, Object> x = new HashMap<>();
...
x.put("eclipselink.logging.level.sql", "FINE");
...
Persistence.createEntityManagerFactory("MyPersistenceUnit", x);
Then you get SQL logs where you can find expressions like: ALTER TABLE myTab ADD myColumn
...
[EL Fine]: sql: 2015-11-11 16:24:14.042--ServerSession(667237426)--Connection(1844518545)--ALTER TABLE bm_picinfo ADD account BIGINT NOT NULL
[EL Fine]: sql: 2015-11-11 16:24:14.066--ServerSession(667237426)--SELECT 1
[EL Warning]: 2015-11-11 16:24:14.092--ServerSession(667237426)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: column "account" contains null values
In the above example, the new field was defined as
@ManyToOne(targetEntity = BmAccountE.class)
@JoinColumn(name = "account", nullable = false, insertable = false, updatable = false)
public BmAccountE account;
In this case, the decision nullable = false
was the decision.
source to share