5
Example: I have the Person class below:
@Entity
public class Pessoa {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idPessoa;
private String nome;
//getters e setters
}
Hibernate, by default, persists in the bank a String like Varchar, and I want to persist in the database as text and change to the following configuration:
@Entity
public class Pessoa {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idPessoa;
@Column(columnDefinition = "text")
private String nome;
//getters e setters
}
Adding the Annotation "@Column(columnDefinition = "text")" in the String attribute, and for that to persist as text, I am dropping the table already created in the database, and my question is as follows:
There would be a way I don’t need to drop the table in the database, since in a more complex system, this can cause me consistency problems?
I worked on a project that when I had these changes we changed the class and gave a
alter
in the database column. I don’t know if it’s the best way, but it worked. That’s because there were times when Hibernate didn’t update all tables running withupdate
or stop creating some kind of Quence.– Maicon Carraro
@Maiconcarraro I wanted to eliminate this
alter
also, I wanted to be unconcerned with the bank, so that I could enter the bank as little as possible. Even because there are customers who don’t like it very much when we ask for access to the bank– Tiago Ferezin
http://stackoverflow.com/questions/12400825/jpa-data-too-long-for-column-does-not-change
– Maicon Carraro
then @Maiconcarraro my xml ta similar to the tutorial there, and nothing, not yet modified, to have to modify by the database yet
– Tiago Ferezin
The tutorial he’s just simulating,
So in essence the create ( which first drops the table and then recreates) works correctly however the update does not if there is a modification in property metadata
. In the last answer there is another solution, but I do not know if it works.– Maicon Carraro
This type of change is always boring to manage even. I particularly use the flywaydb to manage all DDL for me, I don’t use JPA Provider for this, in it I can manage everything, do roolback of amendments, etc..
– Bruno César
@Brunocésar as uses this flywaydb?
– Tiago Ferezin
@Tiagoferezin he works on the concept of migrates. You can use pure SQL or java even, describing the migrates, to versioning the base schema. See documentation the idea of it, the articles and such. If it really suits you, I can include a full example of its use.
– Bruno César
@Brunocésar can put as an answer an example, that if it works I put as accepted, but what I really wanted, is something in Hibernate same. if possible
– Tiago Ferezin
The feature of Hibernate to create or change the database as annotations in the entities is only suitable to use in production bases if the system is really very simple and the integrity of the data is not even so important. Most systems aren’t like that. You will probably have to develop or choose a separate process and tool to migrate the database instead of Hibernate for this.
– Caffé