stackMVC
stackMVC

Reputation: 49

How can I use liquibase with Java code

I am trying use liquibase in a Spring MVC project. I can change database with changelog file but I want to change database with Java code. Liquibase has to see changes from hibernate and liquibase warn me for this changing. There is no example for this. How can I do this. Is there any example?

I create database like this in my project. When I do this(or add column for example), liquibase sees this changing from hibernate files and give me warn commit or rollback for example. How can I do that?

@Entity
@Table(name="strategy")
public class Strategy {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(unique=true, nullable=false)

    private Integer id;
    @Column
    private String type;
    @Column
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Upvotes: 2

Views: 2293

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81930

If I understand your question correctly you are wondering how to integrate the changes to your database schema coming from the Liquibase changelog with those coming from Hibernate Schema generation.

The answer is: You don't

The whole idea of tools like Liquibase is to change the database schema exclusively through the tool.

So if you change a Hibernate entity, say by adding a property, you add a change to the Liquibase changelog to keep both in sync. During normal development, it is probably easiest to do that manually.

If you have many changes you can create a changelog from an existing database, so you could generate your database using the Hibernate schema update feature, and then use this Liquibase feature to create a changelog. But note that this will not create the incremental change sets, that you need to be able to apply the changes to existing database, that already have part of the schema created.

Update based on the comment by @xeoterracide

There is a liquibase hibernate extension which might be helpful. From it's readme:

This Liquibase extension lets you use your Hibernate configuration as a comparison database for diff, diffChangeLog and generateChangeLog. Normal workflow using this extension is:

  1. Edit your Hibernate mapped classes as needed (add and remove classes and attributes)
  2. Run

    liquibase --changeLogFile=changelog.xml --url=jdbc:yourdatabase --referenceUrl=hibernate:classic:path/to/hibernate.cfg.xml diffChangeLog
    
  3. Check that the modified changelog.xml does what you expect, edit it if it does not
  4. Run

    liquibase --changeLogFile=changelog.xml --url=jdbc:yourdatabase update
    
  5. Repeat

If you specify an existing changeLogFile, each run of diffChangeLog will append to the file.

Upvotes: 4

Related Questions