JPA with SQLite autoincrement troubles

Apr 3, 2014

Today i’ve got an exception when JPA was trying to insert a new row to a table.

My model definition was like this: ```java @Entity public class Merchant { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; // ... public Merchant(){ } // ... ```

And the table creation script was the following: ```sql CREATE TABLE MERCHANT ( ID INTEGER PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, BANKNAME TEXT NOT NULL, SWIFT TEXT NOT NULL, ACCOUNT TEXT NOT NULL, CHARGE REAL NOT NULL, PERIOD INTEGER NOT NULL, MINSUM REAL NOT NULL, TOTAL REAL NOT NULL ); ```

I faced some strange exceptions like id could not be null, SQL error or missing database (near “values”: syntax error), etc.

The solution to this consists of two steps:

  • in database: remove the AUTOINCREMENT and NOT NULL annotations from the ID column as SQLite will automatically increment its value (in DB)
  • in entity’ code: remove the GenerationStrategyType from the id member annotation

incompatible character encodings: UTF-8 and ASCII-8BIT

Aug 16, 2012

Just got the following error in my Rails project (Rails 3.2.6, Ruby 1.9.3):

ActionView::Template::Error (incompatible character encodings: UTF-8 and ASCII-8BIT)

That was caused by the case when MySQL record (ActiveReciord object, actually) contained UTF-8 characters and i was trying to output those chars in my template. But mysql gem does not support those. It needs some hacking.

Luckily, there is more convenient way to solve the problem. The solution of this issue now appears too easy:

  • Install the mysql2 gem
  • Use adapter: mysql2 instead of adapter: mysql in your config/database.yml file

My coursework. Two months later

Apr 23, 2012


My coursework: two months later. No, i was not doing all that stuff for two months - i… “freezed” the project for that time =P Actually, the greatest portion of job was done in one day - 22.04.2012 .. 23.04.2012 =)