2014-11-06 43 views
3

我正在使用JPA2,EclipseLink。我正在尝试向现有数据库和表中添加一个新列。我在POJO类MyTable中添加了一个新字段。尽管有create-or-extend-tables属性,新列没有得到加起来。JPA2:create-or-extend-tables没有将新表扩展为新列

这是我的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> 

我不知道为什么新列没有被添加。我找回以下错误:

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 

jar文件我使用:

eclipselink.jar (2.5.2) 
javax.persistence_2.1.0.v201304241213.jar 

请让我知道,如果任何事情在我persistence.xml文件丢失。

任何帮助被赞赏。

更新: 我已经调试到EclipseLink代码,发现它不试图产生新列,它在EntityManagerFactoryProvider类在extendDefaultTables()方法将引发错误,并说表已经存在,并从该方法返回。

DatasourceAccessor类的incrementCallCount()方法中有一个名为useExternalConnectionPooling的字段,如果我手动将此字段设置为true。然后新列成功生成。由于该字段为假,EclipseLink未触发扩展。我不确定,如果这是正确的领域。

+0

请参阅http://stackoverflow.com/questions/13372213/eclipselink-create-or-extend-tables-not-working-unknown-column – 2014-11-06 08:42:09

+0

这并不适用于我。我仍然看到同样的问题。 – User12111111 2014-11-06 09:46:23

+0

表创建的SQL显示什么? – Chris 2014-11-06 14:59:31

回答

0

设置eclipselink.logging.level.sqlFINE,1)通过调整配置文件,或2)程序:

Map<String, Object> x = new HashMap<>(); 
... 
x.put("eclipselink.logging.level.sql", "FINE"); 
... 
Persistence.createEntityManagerFactory("MyPersistenceUnit", x); 

然后你会得到的SQL日志,在那里你可以找到之类的语句: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 

在上面的例子中,新的字段被定义为

@ManyToOne(targetEntity = BmAccountE.class) 
    @JoinColumn(name = "account", nullable = false, insertable = false, updatable = false) 
    public BmAccountE account; 

在这种情况下,解决方案是删除nullable = false