Steps to reproduce:
1. Create the class:
@Entity
@Table(catalog = "testmysql", name = "city")
public class CityEntity {
public CityEntity() {
}
private String m_cityname;
@Basic
@Column(name = "CityName", length = 15)
public String getCityname() {
return m_cityname;
}
public void setCityname(String cityname) {
m_cityname = cityname;
}
private long m_Id;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return m_Id;
}
public void setId(long id) {
m_Id = id;
}
}
2. Create the persistance.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="NewPeristenceUnit">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>testmysql.server.entity.CityEntity</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings"
value="refresh(primaryKeys=true, ForeignKeys=true)"/>
<property name="openjpa.ConnectionURL"
value="jdbc:mysql://localhost:33306/testmysql2"/>
<property name="openjpa.ConnectionDriverName"
value="com.mysql.jdbc.Driver"/>
<property name="openjpa.ConnectionUserName" value="root"/>
<property name="openjpa.ConnectionPassword" value="12345"/>
<property name="openjpa.Log" value="DefaultLevel=TRACE,
Tool=INFO"/>
</properties>
</persistence-unit>
</persistence>
3. Create the test method:
private String JPATest() {
EntityManagerFactory factory = null;
EntityManager manager = null;
try {
factory =
Persistence.createEntityManagerFactory("NewPeristenceUnit");
manager = factory.createEntityManager();
CityEntity city = new CityEntity();
manager.getTransaction().begin();
manager.persist(city);
manager.getTransaction().commit();
}
catch{...}
4. Run the test. In specified table in database are now 2 columns id and
CityName.
5. Now delete all strings in CityEntity.java so, that entity CityEntity
hasn't field CityName more:
@Entity
@Table(catalog = "testmysql", name = "city")
public class CityEntity {
public CityEntity() {
}
private long m_Id;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return m_Id;
}
public void setId(long id) {
m_Id = id;
}
}
6. Run test again.
7. Column CityName is still present in database table. But documentation for
MappingTool about refresh schemaAction says:
"refresh: Equivalent to retain, then add.
retain: Keep all schema components in the given XML definition, but drop the
rest from the database. This action never adds any schema components. "
Unnecessary columns should be deleted in database since the entity doesn't
contain mapped fields for them, when schemaAction is refresh.
--
View this message in context:
http://n2.nabble.com/Reverse-Mapping-by-MappingTool-does-n-delete-unnecessary-columns-tp3748079p3748079.html
Sent from the OpenJPA Developers mailing list archive at Nabble.com.