Spring Boot 1.5.8 and Java 8 here. I've followed all the Spring Boot & Liquibase guides and I can't seem to get Liquibase to work.
Here is a link to a GitHub repo for reproducing the issue exactly, but here's the scoop:
I have the following MySQL v8 database that gets created like so ahead of time (before the app runs):
CREATE DATABASE IF NOT EXISTS troubleshooting_db CHARACTER SET utf8 COLLATE utf8_general_ci;
I have the following src/main/resources/db/changelog files:
db.changelog-master.yaml:
===
databaseChangeLog:
- include:
file: db/changelog/1-setup.sql
1-setup.sql:
===
--liquibase formatted sql
--changeset troubleshooting:1 dbms:mysql
-- LOOKUPS
CREATE TABLE IF NOT EXISTS metric_range_categories (
metric_range_category_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
metric_range_category_ref_id VARCHAR(36) NOT NULL,
metric_range_category_name VARCHAR(250) NOT NULL,
metric_range_category_label VARCHAR(250) NOT NULL,
metric_range_category_description VARCHAR(500) NOT NULL,
CONSTRAINT pk_metric_range_categories PRIMARY KEY (metric_range_category_id),
INDEX idx_metric_range_categories_metric_range_category_ref_id (metric_range_category_ref_id),
INDEX idx_metric_range_categories_metric_range_category_label (metric_range_category_label),
CONSTRAINT uc_metric_range_categories_metric_range_category_ref_id UNIQUE (metric_range_category_ref_id),
CONSTRAINT uc_metric_range_categories_metric_range_category_name UNIQUE (metric_range_category_name),
CONSTRAINT uc_metric_range_categories_metric_range_category_label UNIQUE (metric_range_category_label)
);
-- Lots of other CREATE TABLE statements down here...
And the following JPA-annotated entity:
#Entity
#Table(name = "metric_range_categories")
#AttributeOverrides({
#AttributeOverride(name = "id", column = #Column(name = "metric_range_category_id")),
#AttributeOverride(name = "refId", column = #Column(name = "metric_range_category_ref_id")),
#AttributeOverride(name = "name", column = #Column(name = "metric_range_category_name")),
#AttributeOverride(name = "label", column = #Column(name = "metric_range_category_label")),
#AttributeOverride(name = "description", column = #Column(name = "metric_range_category_description"))
})
public class MetricRangeCategory extends BaseLookup {
public MetricRangeCategory() {
}
public MetricRangeCategory(Long id, String refId, String name, String label, String description) {
super(id, refId, name, label, description);
}
}
At runtime I get the following exception:
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [metric_range_categories]
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateTable(SchemaValidatorImpl.java:67)
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.doValidation(SchemaValidatorImpl.java:50)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:91)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:475)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)
... 29 common frames omitted
So when it starts up, Liquibase does not excute/engage and so Hibernate JPA validation fails because its looking for a table that doesn't exist (because Liquibase never kicked in and did its job!). Any ideas as to where I'm going awry? Why isn't Liquibase kicking in?
There are 2 different problems in the repo:
Wrong location of application.yml. Move it from root to
src/main/resources
Nested property in TroubleshootingConfig.Machine
has null value, because of this bean "authInfo" is not created and context initialization fails. Here is the reference on how Spring Boot Configuration Binding works.
Related
I'm developing an application with Spring Boot and hibernate, which connects to a postgres instance running in docker. When I first created my schema.sql, it looked like this:
CREATE TABLE groups(
group_id varchar(255) PRIMARY KEY,
group_desc varchar(255),
group_name varchar(255)
);
The table created successfully, however I soon realized 255 is too short for my purposes and changed my schema to the following:
CREATE TABLE groups(
group_id text PRIMARY KEY,
group_desc text,
group_name text
);
However, the database keeps reverting to the original data types. I've tried dropping the table, however when the Spring app runs and it gets created again as varchar(255) instead of text. How do I force hibernate to use the updated schema?
I've tried changing the spring.jpa.hibernate.ddl-auto property to create and update, and tried changing the fields to other datatypes, including other lengths of varchar. Nothing has worked so far. Even deleting schema.sql seemingly has no effect.
My application.properties looks like this:
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=<redacted>
spring.datasource.password=<redacted>
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
The Text datatype is not a Varchar but a CLOB.
Check your Groups class. I guess the name and desc attributes are String and the default related db type is then VARCHAR(255). Thus, if you generate your schema from your entity, String always become VARCHAR(255)
If you want to use Text, your field should be annotated with #Lob
public class Groups {
#Lob
#Column(name = "group_desc")
private String desc
#Lob
#Column(name = "group_name")
private String name
}
This being stated, I think you should change your java/db model because having a Lob/Text type as primary/foreign key frightens me a little (even if I never tried)
CREATE TABLE groups(
group_id bigint PRIMARY KEY,
group_code text NOT NULL UNIQUE,
group_desc text,
group_name text
);
public class Groups {
#Id
#Column(name = "group_id")
private Long id;
#Lob
#Column(name = "group_code", nullable = false, unique = true)
private String code;
#Lob
#Column(name = "group_desc")
private String desc;
#Lob
#Column(name = "group_name")
private String name;
}
NB : I usually don't generate the schema from entities so I instead use the code below to map a db text field to entity attribute :
#Column(name = "group_desc", columnDefinition = "CLOB")
private String desc;
But I am not sure if this is handled correctly to generate the schema
I'm using Hibernate 3.6.8 and I have a table defined in mysql (5.5) like this:
CREATE TABLE mytable
(
id BIGINT(20) PRIMARY KEY NOT NULL,
version INT(11),
description VARCHAR(60),
scheduled_at DATETIME,
`from` BIGINT(20),
`to` BIGINT(20),
deleted BIT(1) DEFAULT b'0',
completed BIT(1) DEFAULT b'0',
delete_from_after_completion BIT(1) DEFAULT b'0',
CONSTRAINT FK14F71A73C588I54F FOREIGN KEY (`from`) REFERENCES other_table (id),
CONSTRAINT FK14F71C231C45J198 FOREIGN KEY (`to`) REFERENCES other_table (id)
);
CREATE INDEX FK14F71A73C588I54F ON mytable (`from`);
CREATE INDEX FK14F71C231C45J198 ON mytable (`to`);
And a Java entity defined like this:
#Entity
public class MyTable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "id")
private Long id;
#Version
#Column(name = "version")
private Integer version;
#Column(name = "description")
private String description;
#Column(name = "scheduled_at")
#Temporal(TemporalType.TIMESTAMP)
#DateTimeFormat(style = "SM")
private Date scheduledAt;
#Column(name = "from")
private Long fromId;
#Column(name = "to")
private Long toId;
#Column(name = "deleted")
private boolean deleted;
#Column(name = "completed")
private boolean completed;
#Column(name = "delete_from_after_completion")
private boolean deleteFromEntityAfterCompletion;
...
}
When I try to persist an instance of MyTable with valid values I end up with the following error:
2016-01-12 10:06:33,443 [qtp2139431292-20] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1064, SQLState: 42000
2016-01-12 10:06:33,443 [qtp2139431292-20] ERROR org.hibernate.util.JDBCExceptionReporter - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from, scheduled_at, to, version) values (0, 1, 0, 'Test', 10, '2016-01-13 00:00:' at line 1
I suspect that the problem occurs since I try to model the foreign key relationships (to and from) as Long instead of an entity (OtherTable). I suspect this because Hibernate can indeed persist this entity if I comment out the to and from fields. Note that the relationship to the to and from entities does indeed exists in the database so that's not the problem.
If I try insert manually using something like this it works:
insert into mytable values(3, 0, "desc", '2016-10-10 00:00:', 10, 11, 0, 0, 0);
You have a column that is a SQL reserved keyword ("from") and Hibernate doesn't bother quoting it for you. Other JPA implementations (e.g DataNucleus JPA) take care of such things for you. You will have to add single quotes around the reserved word in your JPA annotation information
I am pretty sure that the problem is not a matter of persisting Long values.
Your code has something wrong. I bet there is a wrong quotation mark in your insert code.
Im getting following error when the application tries to insert a record row in the db.
SQL Error: 1062, SQLState: 23000
ERROR org.hibernate.util.JDBCExceptionReporter - Duplicate entry '7089' for key 'PRIMARY'
ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
Caused by: java.sql.BatchUpdateException: Duplicate entry '7090' for key 'PRIMARY'
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1269)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:955)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
The definition is
#Id
#Column(name = "CST_CUSTOMER_ID_PK")
#GenericGenerator(name = "generator", strategy = "increment")
#GeneratedValue(generator = "generator")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
The part at which the error occurs is :
savedCustomer = customerDao.save(customer);
mtmrsLogger.debug("saved customer id:" + savedCustomer.getId());
/**
* Updating Customer Trans Table
*/
updateCustomerTransForMakerChecker(customer, customerform.getAuditDetails());
/**
* Updating Customer Audit
*/
updateCustomerAuditForMakerChecker(customer, customerform.getAuditDetails());
//status=1;
//Add customer ewallet account
updateCustomerInWalletBalance(customer, customerform.getAuditDetails());
//send sms to customer
smsManager.sendSMSToCUCustomer(customer.getMobileno(), userBean);
}
mtmrsLogger.exiting("CustomerManagerImpl", "addCustomer");
My log shows, program has reached ' Exiting Class: CustomerManagerImpl Method: addCustomer' this part. Im saving the customer ,im setting the same entry in other two tables. The primary key of customer table is foreigh key in other two tables. Im lost please help.
CREATE TABLE `CST_CUSTOMER_INFO` (
`CST_CUSTOMER_ID_PK` bigint(11) NOT NULL AUTO_INCREMENT,
`CST_MOBILE` varchar(30) DEFAULT NULL,
`CST_FIRST_NAME` varchar(50) DEFAULT NULL,
`CST_LAST_NAME` varchar(150) NOT NULL,
`CST_MIDDLE_NAME` varchar(50) DEFAULT NULL,
PRIMARY KEY (`CST_CUSTOMER_ID_PK`)
) ENGINE=InnoDB AUTO_INCREMENT=4103 DEFAULT CHARSET=latin1
Im getting error occassionally in production , but in local its ok..
As table is being modified by more than one application, use of #GenericGenerator(name = "generator", strategy = "increment") will result in ambiguity.
Explanation
Strategy increment : It generates identifiers of type long, short or
int that are unique only when no other process is inserting data into
the same table. It should not be used in the clustered environment.
So you should rethink what strategy is to be used to generate id's. Using sequence or native strategy can resolve your problem.
Replace
`CST_CUSTOMER_ID_PK` bigint(11) NOT NULL AUTO_INCREMENT,
with
`CST_CUSTOMER_ID_PK` bigint(11) NOT NULL,
You are incrementing the id using hibernate/java, no need to make the DBMS increment it also.
I have a small local H2 database whose content has been create with DataNucleus' JDO implementation. It contains a rawcontainitem table, associated to the following object:
#PersistenceCapable(objectIdClass=RawItemKey.class)
#Index(name="CONTAIN_IDX", members={"prefix", "language", "value"})
public class RawContainItem {
#PrimaryKey
#Column(length=40)
String prefix = "";
#PrimaryKey
#Column(length=2)
String language = "";
#PrimaryKey
#Column(length=Integer.MAX_VALUE)
String value = "";
public RawContainItem(String prefix, String language, String value) {
this.prefix = prefix;
this.language = language;
this.value = value;
}
}
This table currently contains several rows where language="FR". I want to add some more rows with language="EN", but I get strange error messages.
When datanucleus.autoCreateSchema is set to true, I get:
INFO: Managing Persistence of Class : net.dwst.findword.DataNucleus.RawContainItem [Table : RAWCONTAINITEM, InheritanceStrategy : new-table]
23-mars-2012 14:42:49 org.datanucleus.store.rdbms.table.AbstractTable create
INFO: Creating table RAWCONTAINITEM
23-mars-2012 14:42:50 org.datanucleus.store.rdbms.table.AbstractTable executeDdlStatementList
GRAVE: Error thrown executing CREATE TABLE RAWCONTAINITEM
(
"LANGUAGE" VARCHAR(2) NOT NULL,
PREFIX VARCHAR(40) NOT NULL,
"VALUE" VARCHAR(2147483647) NOT NULL,
CONSTRAINT RAWCONTAINITEM_PK PRIMARY KEY ("LANGUAGE",PREFIX,"VALUE")
) : Table "RAWCONTAINITEM" already exists; SQL statement:
CREATE TABLE RAWCONTAINITEM
(
"LANGUAGE" VARCHAR(2) NOT NULL,
PREFIX VARCHAR(40) NOT NULL,
"VALUE" VARCHAR(2147483647) NOT NULL,
CONSTRAINT RAWCONTAINITEM_PK PRIMARY KEY ("LANGUAGE",PREFIX,"VALUE")
) [42101-164]
org.h2.jdbc.JdbcSQLException: Table "RAWCONTAINITEM" already exists;
...
This message is true, the table exists indeed.
When datanucleus.autoCreateSchema is set to false, I get:
Required table missing : "RAWCONTAINITEM" in Catalog "" Schema "".
DataNucleus requires this table to perform its persistence operations.
Either your MetaData is incorrect, or you need to enable "datanucleus.autoCreateTables"
org.datanucleus.store.rdbms.exceptions.MissingTableException:
Required table missing : "RAWCONTAINITEM" in Catalog "" Schema "".
DataNucleus requires this table to perform its persistence operations.
Either your MetaData is incorrect, or you need to enable "datanucleus.autoCreateTables"
...
But the table does exists... What gives?
I'm testing JPA, in a simple case File/FileVersions tables (Master/Details), with OneToMany relation, I have this problem: in FileVersions table, the field "file_id" (responsable for the relation with File table) accepts every values, not only values from File table.
How can I use the JPA mapping to limit the input in FileVersion.file_id only for values existing in File.id?
My class are File and FileVersion:
FILE CLASS
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="FILE_ID")
private Long id;
#Column(name="NAME", nullable = false, length = 30)
private String name;
//RELATIONS -------------------------------------------
#OneToMany(mappedBy="file", fetch=FetchType.EAGER)
private Collection <FileVersion> fileVersionsList;
//-----------------------------------------------------
FILEVERSION CLASS
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name="VERSION_ID")
private Long id;
#Column(name="FILENAME", nullable = false, length = 255)
private String fileName;
#Column(name="NOTES", nullable = false, length = 200)
private String notes;
//RELATIONS -------------------------------------------
#ManyToOne(fetch=FetchType.EAGER)
#JoinColumn(name="FILE_ID", referencedColumnName="FILE_ID", nullable=false)
private File file;
//-----------------------------------------------------
and this is the FILEVERSION TABLE
CREATE TABLE `JPA-Support`.`FILEVERSION` (
`VERSION_ID` bigint(20) NOT NULL AUTO_INCREMENT,
`FILENAME` varchar(255) NOT NULL,
`NOTES` varchar(200) NOT NULL,
`FILE_ID` bigint(20) NOT NULL,
PRIMARY KEY (`VERSION_ID`),
KEY `FK_FILEVERSION_FILE_ID` (`FILE_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
Thanks for help,
I know the SQL constraint to limit the input, but it is possible to create this SQL costraint using some annotation, without writing by hand the SQL in the database?
I'm new on JPA, I was thinking that using #JoinColumn annotation, JPA could create also the costraint...
Thank you again.
At the Java level, you describe and annotate associations between classes - which and you did - and your mapping looks fine.
At the database level, if you want to restrict the possible values in the file_id column to values that are primary keys in the FILE table, you should use a foreign key constraint. To do so, you will need to use InnoDB tables. Something like that:
CREATE TABLE `JPA-Support`.`FILEVERSION` (
`VERSION_ID` bigint(20) NOT NULL AUTO_INCREMENT,
`FILENAME` varchar(255) NOT NULL,
`NOTES` varchar(200) NOT NULL,
`FILE_ID` bigint(20) NOT NULL,
PRIMARY KEY (`VERSION_ID`),
FOREIGN KEY `FK_FILEVERSION_FILE_ID` (`FILE_ID`) REFERENCES FILE(ID)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
The table FILE also has to use InnoDB. Actually, use InnoDB tables for the tables for which you want to use referential integrity.