mardi 31 mars 2015

Why JPA declaration of unique multiple constrains works in mySQL but not in SQLite

I have the following entity bean defined with JPA



@Entity
@Table(name = "Person", schema = "", uniqueConstraints = {
@UniqueConstraint(columnNames = {"Name", "Type"}),
@UniqueConstraint(columnNames = {"PersonID"})})
public class PersonDataBean implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "PersonID", nullable = false)
private Integer personID;
@Column(name = "Name", length = 255, nullable = false)
private String name;
@Column(name = "Type", length = 255, nullable = false)
private String type;

//getters and setters
}


I make use of the javax.persistence.EntityManager objects for accessing the database. I've made some unit tests to verify that records with the same name but different type can be correctly inserted in the database, and records with the same name AND type throw exceptions, as it is defined in the code above. When I make a connection to a mySQL database, everything works as expected. However, when I use the same JPA entity object with the same unit test in SQLite, then records with the same name and type can be wrongly added. My first impression was that SQLite does not support unique constraints on multiple columns. However, I read here Sqlite table constraint - unique on multiple columns that this is supported. During the initialization of the EntityManager, I can read from the logs the following auto-generated statement for the table creation



CREATE TABLE Person (PersonID INTEGER NOT NULL, Name VARCHAR(255) NOT NULL, Type VARCHAR(255) NOT NULL PRIMARY KEY (PersonID))
ALTER TABLE Person ADD CONSTRAINT UNQ_Person_0 UNIQUE (Name, Type)


Any ideas why this is happening?


Aucun commentaire:

Enregistrer un commentaire