h2 database muddling order of columns

It seems that when using @Entity on a class to provide the schema for the h2 database, the order of the columns does not follow the order you’ve specified the class members.

I have a class called article:

package com.backtojavaland.article;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Getter;
import lombok.Setter;

@Entity
@Setter @Getter
public class Article {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int articleId;
	
	private String title;
	private String summary;

	public Article() {}
	
	public Article(String title, String summary) {
		this.title = title;
		this.summary = summary;
	}
}

I have a data.sql file to insert some data to start off with:

INSERT INTO article values(1, 'Nasa 2020 robot rover to target Jezero ''lake'' crater', 'America''s next robot rover will be sent to a 50km-wide depression that once had water running through it.');

Note: The single quotes and apostrophes have to be escaped.

However, the data wasn’t right. The title and summary were swapped around. To sort this out I had to specify the column names in the INSERT statement. I suppose its much better practice to do this anyway.

INSERT INTO article (article_id, title, summary) values(1, 'Nasa 2020 robot rover to target Jezero ''lake'' crater', 'America''s next robot rover will be sent to a 50km-wide depression that once had water running through it.');

I also had to remove my schema.sql file as this wasn’t loading anything into the database. I’m sure it worked before. Oh well.

Leave a comment