Hello everybody:
I'm using JPA-Toplink in a desktop application, linked to a Postgres DB. I'm
following a course and its exercises. I created the tables from the code
without problems. The problems come whenever I want to add more attributes
to the already existing classes (In the next exercise we create
relationships between classes made with new attributes).
Having the 2 classes:
@Entity
public class Autor implements Serializable {
@Id
private String nombre;
private String correo;
@OneToMany(mappedBy = "autor")
private Collection<Mensaje> mensajes = new HashSet<Mensaje>();
//This is the new attribute for the new relationship
@OneToMany(mappedBy="autor")
private Collection<PaginaHtml> paginas = new HashSet<PaginaHtml>();
...}
@Entity
public class PaginaHtml implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String nombreFichero;
@Temporal(value = TemporalType.DATE)
private Calendar creacion;
@Lob
private String html;
private Encoding encoding;
private String extensión;
//This is the new attribute for the new relationship
@ManyToOne
private Autor autor;
...}
And the code to link both:
public void addAutorPagina(){
PaginaHtmlDAO phDAO = new PaginaHtmlDAO (em);
AutorDAO autDAO = new AutorDAO (em);
List<PaginaHtml> listaPag = new ArrayList();
List<Autor> listaAut = new ArrayList();
em.getTransaction().begin(); //3
listaPag = phDAO.listaPaginas();
listaAut = autDAO.listaAutores();
if (!listaPag.isEmpty() && !listaAut.isEmpty()){
listaPag.get(0).setAutor(listaAut.get(0)); //Update relationship
from propietary end (PaginaHtml)
listaAut.get(0).addPagina(listaPag.get(0)); //Update relationship
from other end
}
em.getTransaction().commit(); //4
}
Whenever I do the commit, I get the error from Postgre:
"Internal Exception: org.postgresql.util.PSQLException: ERROR: no existe la
columna «autor_nombre»" (The column "autor_nombre" doesn't exist.
This is done with my Table Generation Strategy (from the PU) set to
"Create".
If I change this strategy to "Drop and Create", I don't get any errors, and
the column "autor_nombre" is created in the table "paginahtml".
But for me this is not a solution, since when I drop the table I loose all
the data...
is that the correct procedure? Every time you change attributes in classes
you have to backup your DB, change to "Drop and Create", and then get back
the "old data".
Or what am I doing wrong?
Tank you very much for your time,
Pere Serrano
--
View this message in context: http://www.nabble.com/adding-new-attributes-to-classes-won%27t-create-the-respective-columns-in-database-tp17986273p17986273.html
Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.