Micronaut Data: INSERT of Parent-Child relationship with autogenerated Parent ID fails cause Foreign Key not set - micronaut-data

stumbled over a problem: Within One-to-Many relationship it seems that auto-generated primary key from parent entity is NOT fed to child entity.
I've boiled down production code to following example:
Entities, One-to-Many relationship of Parent and Child, both with DB-generated primary key fields (Kotlin code):
import io.micronaut.data.annotation.GeneratedValue
import io.micronaut.data.annotation.Id
import io.micronaut.data.annotation.MappedEntity
import io.micronaut.data.annotation.Relation
import io.micronaut.data.annotation.Relation.Cascade.PERSIST
import io.micronaut.data.annotation.Relation.Kind.ONE_TO_MANY
import io.micronaut.data.model.naming.NamingStrategies
#MappedEntity("Parent", namingStrategy = NamingStrategies.Raw::class)
data class Parent(
#GeneratedValue #field:Id val id: Long?,
val name: String,
#field:Relation(ONE_TO_MANY, mappedBy = "parent_id", cascade = [PERSIST])
val children: List<Child>? = emptyList()
) {
// Copy-constructor, used by ORM:
constructor(
name: String,
children: List<Child>?
) : this(
null,
name,
children
)
}
#MappedEntity("Child", namingStrategy = NamingStrategies.Raw::class)
data class Child(
#GeneratedValue #field:Id val id: Long?,
val parent_id: Long?,
val description: String,
) {
// Copy-constructor, used by ORM:
constructor(
description: String,
) : this(null, null, description)
}
Repository code to persist Parent entities (Kotlin):
import io.micronaut.data.annotation.Repository
import io.micronaut.data.jdbc.annotation.JdbcRepository
import io.micronaut.data.model.query.builder.sql.Dialect.H2
import io.micronaut.data.repository.GenericRepository
import javax.transaction.Transactional
#Repository
#JdbcRepository(dialect = H2)
interface ParentRepository : GenericRepository<Parent, Long> {
#Transactional
fun save(parent: Parent): Parent
}
Now the test case. It defines DB schema and creates it with in-memory H2 database. It tries to persist a single Parent instance with one Child (written with Spock/Groovy):
import groovy.sql.Sql
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification
import javax.sql.DataSource
#MicronautTest
class ParentRepositorySpec extends Specification {
#Inject
DataSource dataSource
#Inject
ParentRepository parentRepository
def 'Inserting parent with single child'() {
given:
loadDatabaseSchemaTo(dataSource)
def child = new Child('Single child')
def parent = new Parent('Parent', List.of(child))
when:
def savedParent = parentRepository.save(parent)
then:
savedParent.id != null // Primary Key field gets populated
savedParent.items[0].id != null // Same with primary key field of child item
}
static void loadDatabaseSchemaTo(DataSource dataSource) {
def createTableParent = '''
CREATE TABLE `Parent` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
)'''
def createTableChild = '''
CREATE TABLE `Child` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`parent_id` int(5) NOT NULL, -- <= Mandatory foreign key
`description` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`parent_id`) REFERENCES `Parent` (`id`)
)'''
new Sql(dataSource).with { sql ->
sql.execute(createTableParent)
sql.execute(createTableChild)
}
}
}
Child tables foreign key field "parent_id" is defined as MANDATORY: No Child entity without Parent.
But executing test case throws:
io.micronaut.data.exceptions.DataAccessException: SQL Error executing INSERT: SQL error executing INSERT: NULL not allowed for column "PARENT_ID"; SQL statement:
INSERT INTO `Child` (`parent_id`,`description`) VALUES (?,?) [23502-200]
Logging shows that INSERT to table "Parent" is executed followed by "Batch SQL Insert" to table "Child" ... which breaks.
So to me it seems that auto-generated primary key from Parent entity is NOT being propagated to Child entity's field "parent_id" prior to issuing the INSERT statements targeting table "child".
What is missing in my code?

Related

How to convert sqlite data to room database?

we already created sqlite database. this database we stored 500 000 up data but now we want to convert those data on room database. How to stored those all data in room database?
Migrate from SQLite to Room
Example : 😎
SQLiteOpenHelper implementation
User.java
public class User {
private int uId;
private String uName;
private String uContact;
public User() {
}
public User(int id, String name, String number){
this.uId = id;
this.uName = name;
this.uContact= number;
};
//getters setters left out for brevity
}
UserDbHelper.java
public class UserDbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "userDB";
// user table name
private static final String TABLE_USERS = "users";
// user Table Columns names
private static final String USER_ID = "user_id";
private static final String USER_NAME = "user_name";
private static final String USER_PH_NO = "user_contact";
public UserDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
//create the table if not yet created
String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USERS + "("
+ USER_ID + " INTEGER PRIMARY KEY," + USER_NAME + " TEXT,"
+ USER_PH_NO + " TEXT" + ")";
db.execSQL(CREATE_USER_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
// Create tables again
onCreate(db);
}
public void addUser(User user){
//Add new user to database
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(USER_NAME, user.getUName()); // Contact Name
values.put(USER_PH_NO, user.getUContact()); // Contact Phone
values.put(USER_ID, user.getUId());
// Inserting Row
db.insert(TABLE_USERS, null, values);
db.close(); // Closing database connection
}
public User getUser(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_USERS, new String[] { USER_ID,
USER_NAME, USER_PH_NO }, USER_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
User contact = new User(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return contact;
}
}
add data
//get database instance
UserDbHelper userDbHelper = new UserDbHelper(this);
//add couple of users
userDbHelper.addUser(new User(1,"Hulk","11-445-9999"));
userDbHelper.addUser(new User(2,"Dominic","11-445-9999"));
And fetch the users using this.
User extractedUser = userDbHelper.getUser(1);
Migrating to Room:
User.java
#Entity
public class User {
#PrimaryKey
private int uId;
private String uName;
private String uNumber;
public User() {
}
#Ignore
public User(int id, String name, String number){
this.uId = id;
this.uName = name;
this.uNumber= number;
};
//add all the getters and setters here
}
UserDAO.java
#Dao
public interface UserDAO {
#Insert(onConflict = REPLACE)
void insertUser(User user);
#Query("SELECT * FROM User")
List<User> getUsers();
}
UserDB.java
#Database (entities = {User.class},version = 2)
public abstract class UserDB extends RoomDatabase {
public abstract UserDAO userDAO();
}
Migration instance
final Migration MIGRATION_1_2 = new Migration(1, 2) {
#Override
public void migrate(SupportSQLiteDatabase database) {
// Since we didn't alter the table, there's nothing else to do here.
}
};
//create db
final UserDB userDB = Room.databaseBuilder(this
, UserDB.class
, "userDB")
.addMigrations(MIGRATION_1_2)
.build();
//inserting and accessing data using DAO
//this operations needs to be performed on thread other than main thread
userDB.userDAO().insertUser(new User123(5,"Wowman", "888888888"));
userDB.userDAO().insertUser(new User123(6,"Captain", "888888888"));
User user = userDB.userDAO().getUsers().get(0);
Table
#ColumnInfoname(name="user_id")
private int uId;
For full demo https://github.com/amit-bhandari/RoomSamples
This is a potentially difficult task as Room has quite specific requirements for column definitions (the dreaded expected .... found ....).
In short Room can only have column types that are one off INTEGER, REAL, TEXT. BLOB. (note that Room does not support the NUMERIC type).
Furthermore column constraints also have to suit what Room expects.
I would suggest that the way to go is to create the #Entity annotated classes (one per table) with suitable members/fields, that matches the original database as closely as possible
Each #Entity annotated class MUST have a PRIMARY KEY either a single member/field annotated with #PrimaryKey or the primary key can be specified using the primaryKeys parameter of the #Entity annotation.
I would suggest after creating the first #Entity annotated class to then create the #Database annotated abstract class that extends the RoomDatabase class. Noting that the entities parameter should be a list of the #Entity annotated class (i.e. indicating which classes/tables are to be used for the database).
After the creation of each (at least at first) I would suggest compiling the project, so as to limit the possible issues that may be encountered.
Once you have all the #Entity annotated classes then you can consider the migration aspect.
As you have pre-existing data you will want to include the original database as an asset. Room can then copy this database from the asset via the createFromAsset method. However, there is a high chance that the tables will need converting to suit Room.
You can convert the tables either prior to creating the asset file or you could convert the tables via the prePackagedDatabaseCallback. I would suggest that the former is simpler as you can use one of the available SQLite Tools.
Obviously you need to know what needs to be converted. Rather to try to explain the rules. Room, after successfully compiling the project, generates the SQL that it uses to create the tables (and indexes and views)
triggers are not something that Room caters for (an exception being for FTS), so if you have triggers then you could add them as part of the conversion.
In the Android View after compiling you will see that in Java(generated) there will be class that is the same as the #Database annotated class but suffixed with Impl. In this class there will be a method createAllTables. This includes the SQL for all of the tables and indexes (ignore tables that start with sqlite_, android_ or room_ as these are system tables and will be created as required).
Basically you create these tables and then copy the data from original. Obviously one set of tables MUST have different names, after the data has been coped you then drop the original tables ending up with the new tables with the original names.
Thus at some stage you will have to rename a set of tables.
You can either
create the new tables different name, copy the data, drop the original tables and then rename the new tables, or
rename the original tables, create the new tables, copy the data and drop the renamed original tables
There is a good chance that you could do the copies using a simple INSERT SELECT. e.g. INSERT INTO new_table SELECT * FROM original_table;
however complexities can arise, again sometimes Room will have expectations that may not suit exactly.
Simple Example
This is based upon a data base with three tables:-
1) The establishment table with 5 columns:-
the establishment_id, a unique integer value that is the primary key.
the establishment_name, which has the name of the establishmnet as a text/string value and has the UNIQUE constraint.
the country, a text/string value.
the state, a text/string value.
the city a text/string value.
However, the column types use the flexibility of SQLite's column types to demonstrate (wrongly or rightfully is debatable and that debate is beyond the scope of the answer).
the DDL being
:-
CREATE TABLE establishment (
establishment_id INTEGER PRIMARY KEY,
establishment_name the_name_of_the_establishment UNIQUE,
country unusualcolumntype,
state VARCHAR(16),
city charvar(32)
);
The table is populated with according to:-
and
2) The classroom table with 3 columns:-
the classroom_id, a unique integer value that is the primary key.
the clasroom_name, a text/string value that has the UNQIUE constraint.
the classroom_capacity, an integer value.
the DDL being
:-
CREATE TABLE classroom (
classroom_id INTEGER PRIMARY KEY,
classroom_name UNIQUE,
classroom_capacity
);
the table is populated according to:-
finally
3) the establishment_classroom_map, an associative/mapping/reference (an various other terms) table that caters for a many-many relationship between establishments and rooms (albeit it unlikely that the same room would be used by multiple establishments). The table has 2 columns:-
the establishment_id_map, an integer value which MUST be an existing value in the establishment_id column of the establishment table (aka a Foreign Key).
the classroom_id_map, likewise an integer value which MUST be an existing value in the classrom_id column of the classroom table, again a Foreign Key.
the columns form a composite primary key
additionally, to add some complexity, the classroom_id_map column is also indexed.
the DDL being
:-
CREATE TABLE establishment_classroom_map (
establishment_id_map INTEGER REFERENCES establishment(establishment_id) ON DELETE CASCADE ON UPDATE CASCADE,
classroom_id_map INTEGER REFERENCES classroom(classroom_id) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY (establishment_id_map,classroom_id_map)
);
CREATE INDEX idx01_establishmentclassroommap_classroom ON establishment_classroom_map (classroom_id_map);
the table is populated as per:-
Using the following query:-
SELECT
establishment.establishment_name,
establishment.city
||','||establishment.state
||','||establishment.country AS establisment_address,
classroom.classroom_name, classroom.classroom_capacity
FROM establishment
JOIN establishment_classroom_map ON establishment.establishment_id = establishment_classroom_map.establishment_id_map
JOIN classroom ON establishment_classroom_map.classroom_id_map = classroom.classroom_id
;
results in:-
This is the Original Database to be converted.
Step1 - Creating the Room #Entity annotated classes (3 of them).
1.a in Android Studio a new empty Kotlin project is created (in this case SO74298106MigratePreRoomDatabase) and the minimal Room dependencies are added via File/Project Structure/Dependencies that result in:-
implementation 'androidx.room:room-ktx:2.5.0-beta01'
implementation 'androidx.room:room-runtime:2.5.0-beta01'
kapt 'androidx.room:room-compiler:2.5.0-beta01'
1.b for brevity and convenience a single file named DBStuff.kt is created (New Kotlin Class/File)
1.c The DBStuff file is changed from nothing to :-
const val ESTABLISHMENT_TABLE_NAME = "establishment"
const val ESTABLISHMENT_TABLE_ID_COLUMN_NAME = ESTABLISHMENT_TABLE_NAME + "_id"
const val ESTABLISHMENT_TABLE_NAME_COLUMN_NAME = ESTABLISHMENT_TABLE_NAME + "_name"
const val ESTABLISHMENT_TABLE_COUNTRY_COLUMN_NAME = "country"
const val ESTABLISHMENT_TABLE_STATE_COLUMN_NAME = "state"
const val ESTABLISHMENT_TABLE_CITY_COLUMN_NAME = "city"
#Entity(
tableName = ESTABLISHMENT_TABLE_NAME,
indices = [
Index(value = [ESTABLISHMENT_TABLE_NAME_COLUMN_NAME], unique = true)
]
)
data class Establishment(
/* as integer type AND annotated with #Primary key then an alias of the rowid */
/* note in theory rowid's can exceed the capacity of an Int, hence Long used */
/* the default value of null, is interpreted by room as not to supply a value and this the value will be generated */
/* it is inefficient to use AutoGenerate = true */
#PrimaryKey
#ColumnInfo(name = ESTABLISHMENT_TABLE_ID_COLUMN_NAME)
var establishment_id: Long?=null,
/* Note the single use of type String for all the various column types in the original */
#ColumnInfo(name = ESTABLISHMENT_TABLE_NAME_COLUMN_NAME)
var name: String, /* no ? so cannot be null */
#ColumnInfo(name = ESTABLISHMENT_TABLE_COUNTRY_COLUMN_NAME)
var country: String,
#ColumnInfo(name = ESTABLISHMENT_TABLE_STATE_COLUMN_NAME)
var state: String,
#ColumnInfo(name = ESTABLISHMENT_TABLE_CITY_COLUMN_NAME)
var city: String
)
#Database(entities = [Establishment::class], exportSchema = false, version = 1)
abstract class TheDatabase: RoomDatabase() {
}
The constants are not required but suggested as a single point for the column, table and database names.
Room will by default use the field/member/class name for the column and table names but you can change these as has been shown via the #ColumnInfo and for tablenames the #Entity annotation.
What Room doesn't cater for is the column's UNIQUE constraint. So to enforce the UNIQUEness a unique index has been specified for the establishment_name column.
Note the #Database annotated class with the Establishment class listed in the entities parameter of the #Database annotation, otherwise no use is yet made of the database. However, if at this stage the project is compiled then:-
i.e. Room has kindly let us know what it expects the exact schema to be.
i.e. how Room interprets the #Entity annotated Establishment class.
Room expects the establishment table to be exactly:-
CREATE TABLE IF NOT EXISTS `establishment` (
`establishment_id` INTEGER,
`establishment_name` TEXT NOT NULL,
`country` TEXT NOT NULL,
`state` TEXT NOT NULL,
`city` TEXT NOT NULL,
PRIMARY KEY(`establishment_id`)
)
not quite correct as establisment_id INTEGER PRIMARY KEY will be consider as the above (noting that the column level use of PRIMARY KEY cannot be used for a composite primary key).
The above does have a potential issue in that all columns have the NOT NULL constraint. If there are null values in the original data then constraint conflicts will result unless action is taken to either ignore (INSERT OR IGNORE INTO ....) or the column is converted e.g. '.... SELECT coalesce(the_column,'default_value'), .... '
as we can see the original data above does not contain nulls so this will not be an issue.
Onto the classroom table/#Entity annotated class, we add the following code:-
const val CLASSROOM_TABLE_NAME = "classroom"
const val CLASSROOM_TABLE_ID_COLUMN_NAME = CLASSROOM_TABLE_NAME + "_id"
const val CLASSROOM_TABLE_NAME_COLUMN_NAME = CLASSROOM_TABLE_NAME + "_name"
const val CLASSROOM_TABLE_CAPACITY_COLUMN_NAME = CLASSROOM_TABLE_NAME + "_capacity"
and
#Entity(
tableName = CLASSROOM_TABLE_NAME,
indices = [
Index(value = [CLASSROOM_TABLE_NAME_COLUMN_NAME], unique = true)
]
)
data class Classroom(
#PrimaryKey
#ColumnInfo(name = CLASSROOM_TABLE_ID_COLUMN_NAME)
var classroomId: Long?=null,
#ColumnInfo(name = CLASSROOM_TABLE_NAME_COLUMN_NAME)
var name: String,
#ColumnInfo(name = CLASSROOM_TABLE_CAPACITY_COLUMN_NAME)
var capacity: Int
)
and then change the #Database annotation to include the Classroom class in the entities parameter:-
#Database(entities = [Establishment::class,Classroom::class], exportSchema = false, version = 1)
and then compile and looking at the generated database class it now includes:-
_db.execSQL("CREATE TABLE IF NOT EXISTS `classroom` (`classroom_id` INTEGER, `classroom_name` TEXT NOT NULL, `classroom_capacity` INTEGER NOT NULL, PRIMARY KEY(`classroom_id`))");
_db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS `index_classroom_classroom_name` ON `classroom` (`classroom_name`)");
onto the establishment_classroom_map** table. This is a little more complex as Room needs to be told about
the Foreign Keys and
the index on the classroom_id_map column (so access via the classroom is more efficient) and
the composite primary key
So the following code is added:-
const val ESTABLISHMENTCLASSROOMMAP_TABLE_NAME = "${ESTABLISHMENT_TABLE_NAME}_${CLASSROOM_TABLE_NAME}_map"
const val ESTABLISHMENTCLASSROOMMAP_TABLE_ESTABLISHMENTIDMAP_COLUMN_NAME = "${ESTABLISHMENT_TABLE_ID_COLUMN_NAME}_map"
const val ESTABLISHMENTCLASSROOMMAP_TABLE_CLASSROOMIDMAP_COLUMN_NAME = "${CLASSROOM_TABLE_ID_COLUMN_NAME}_map"
and
#Entity(
tableName = ESTABLISHMENTCLASSROOMMAP_TABLE_NAME,
foreignKeys = [
ForeignKey(
entity = Establishment::class,
parentColumns = [ESTABLISHMENT_TABLE_ID_COLUMN_NAME],
childColumns = [ESTABLISHMENTCLASSROOMMAP_TABLE_ESTABLISHMENTIDMAP_COLUMN_NAME],
onDelete = ForeignKey.CASCADE,
onUpdate = ForeignKey.CASCADE
),
ForeignKey(
entity = Classroom::class,
parentColumns = [CLASSROOM_TABLE_ID_COLUMN_NAME],
childColumns = [ESTABLISHMENTCLASSROOMMAP_TABLE_CLASSROOMIDMAP_COLUMN_NAME],
onDelete = CASCADE,
onUpdate = CASCADE
)
],
primaryKeys = [ESTABLISHMENTCLASSROOMMAP_TABLE_ESTABLISHMENTIDMAP_COLUMN_NAME, ESTABLISHMENTCLASSROOMMAP_TABLE_CLASSROOMIDMAP_COLUMN_NAME]
)
data class EstablishmentClassroomMap(
#ColumnInfo(name = ESTABLISHMENTCLASSROOMMAP_TABLE_ESTABLISHMENTIDMAP_COLUMN_NAME)
var establishment_id_map: Long,
#ColumnInfo(name = ESTABLISHMENTCLASSROOMMAP_TABLE_CLASSROOMIDMAP_COLUMN_NAME, index = true)
var classroom_id_map: Long
)
foreign keys and composite primary key are defined in the #Entity annotation
and
the index on the classroom_id_map column is defined in the #ColumnInfo annotation
After yet again amending the #Database annotation to now use entities = [Establishment::class,Classroom::class, EstablishmentClassroomMap::class] and then compiling the following is additionally generated:-
_db.execSQL("CREATE TABLE IF NOT EXISTS `establishment_classroom_map` (`establishment_id_map` INTEGER NOT NULL, `classroom_id_map` INTEGER NOT NULL, PRIMARY KEY(`establishment_id_map`, `classroom_id_map`), FOREIGN KEY(`establishment_id_map`) REFERENCES `establishment`(`establishment_id`) ON UPDATE CASCADE ON DELETE CASCADE , FOREIGN KEY(`classroom_id_map`) REFERENCES `classroom`(`classroom_id`) ON UPDATE CASCADE ON DELETE CASCADE )");
_db.execSQL("CREATE INDEX IF NOT EXISTS `index_establishment_classroom_map_classroom_id_map` ON `establishment_classroom_map` (`classroom_id_map`)");
Now the SQL for the creation of the tables that Room expects exists.
Step 2 Converting the original data to suit room.
IT IS STRONGLY SUGGESTED THAT THIS IS DONE USING A COPY OF THE ORIGINAL DATABASE OR THAT THE ORIGINAL DATABASE IS BACKED UP
This is probably best done using an SQLite tool (Navicat for SQlite used in this case). In short you generate and populate the tables created as per the generated SQl after successful compilation.
Here's the query used for the conversion (it includes the original query used above to display the joined data):-
/*
>>>>>>>>>> IMPORTANT <<<<<<<<<<
>>>>>>>>>> COMMENTED OUT AFTER FIRST RUN AS THEY WILL FAIL AFTER RUNNING ONCE <<<<<<<<<<
ALTER TABLE establishment_classroom_map RENAME TO original_establishment_classroom_map;
ALTER TABLE establishment RENAME TO original_establishment;
ALTER TABLE classroom RENAME TO original_classroom;
*/
/* FROM Generated Java (copied here as is so it can be copied from here):-
_db.execSQL("CREATE TABLE IF NOT EXISTS `establishment` (`establishment_id` INTEGER, `establishment_name` TEXT NOT NULL, `country` TEXT NOT NULL, `state` TEXT NOT NULL, `city` TEXT NOT NULL, PRIMARY KEY(`establishment_id`))");
_db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS `index_establishment_establishment_name` ON `establishment` (`establishment_name`)");
_db.execSQL("CREATE TABLE IF NOT EXISTS `classroom` (`classroom_id` INTEGER, `classroom_name` TEXT NOT NULL, `classroom_capacity` INTEGER NOT NULL, PRIMARY KEY(`classroom_id`))");
_db.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS `index_classroom_classroom_name` ON `classroom` (`classroom_name`)");
_db.execSQL("CREATE TABLE IF NOT EXISTS `establishment_classroom_map` (`establishment_id_map` INTEGER NOT NULL, `classroom_id_map` INTEGER NOT NULL, PRIMARY KEY(`establishment_id_map`, `classroom_id_map`), FOREIGN KEY(`establishment_id_map`) REFERENCES `establishment`(`establishment_id`) ON UPDATE CASCADE ON DELETE CASCADE , FOREIGN KEY(`classroom_id_map`) REFERENCES `classroom`(`classroom_id`) ON UPDATE CASCADE ON DELETE CASCADE )");
_db.execSQL("CREATE INDEX IF NOT EXISTS `index_establishment_classroom_map_classroom_id_map` ON `establishment_classroom_map` (`classroom_id_map`)");
*/
/* Just in case the tables exist drop them (they shouldn't but if rerunning they will)*/
DROP TABLE IF EXISTS establishment_classroom_map;
DROP TABLE IF EXISTS establishment;
DROP TABLE IF EXISTS classroom;
CREATE TABLE IF NOT EXISTS `establishment` (
`establishment_id` INTEGER,
`establishment_name` TEXT NOT NULL,
`country` TEXT NOT NULL,
`state` TEXT NOT NULL,
`city` TEXT NOT NULL,
PRIMARY KEY(`establishment_id`)
);
CREATE UNIQUE INDEX IF NOT EXISTS `index_establishment_establishment_name` ON `establishment` (`establishment_name`);
CREATE TABLE IF NOT EXISTS `classroom` (
`classroom_id` INTEGER,
`classroom_name` TEXT NOT NULL,
`classroom_capacity` INTEGER NOT NULL,
PRIMARY KEY(`classroom_id`)
);
CREATE UNIQUE INDEX IF NOT EXISTS `index_classroom_classroom_name` ON `classroom` (`classroom_name`);
CREATE TABLE IF NOT EXISTS `establishment_classroom_map` (
`establishment_id_map` INTEGER NOT NULL,
`classroom_id_map` INTEGER NOT NULL,
PRIMARY KEY(
`establishment_id_map`,
`classroom_id_map`
),
FOREIGN KEY(`establishment_id_map`) REFERENCES `establishment`(`establishment_id`) ON UPDATE CASCADE ON DELETE CASCADE ,
FOREIGN KEY(`classroom_id_map`) REFERENCES `classroom`(`classroom_id`) ON UPDATE CASCADE ON DELETE CASCADE
);
INSERT /* OR IGNORE */ INTO establishment SELECT * FROM original_establishment;
INSERT /* OR IGNORE */ INTO classroom SELECT * FROM original_classroom;
INSERT /* OR IGNORE */ INTO establishment_classroom_map SELECT * FROM original_establishment_classroom_map;
/* MORE EFFICIENT IF LEFT TILL AFTER INSERTS BUT WILL NOT DETECT UNIQUE CONFLICTS */
CREATE INDEX IF NOT EXISTS `index_establishment_classroom_map_classroom_id_map` ON `establishment_classroom_map` (`classroom_id_map`);
SELECT
establishment.establishment_name,
establishment.city
||','||establishment.state
||','||establishment.country AS establisment_address,
classroom.classroom_name, classroom.classroom_capacity
FROM establishment
JOIN establishment_classroom_map ON establishment.establishment_id = establishment_classroom_map.establishment_id_map
JOIN classroom ON establishment_classroom_map.classroom_id_map = classroom.classroom_id
;
/* ONLY INTRODUCE WHEN FULLY CONFIDENT THAT THE CONVERSION WORKS
DROP TABLE IF EXISTS original_establishment_classroom_map;
DROP TABLE IF EXISTS original_establishment;
DROP TABLE IF EXISTS original_classroom;
*/
The end result (output from query) :-
Note as this is just a demo the original tables have not been dropped
The database should then be saved and closed.
Step 3 using the converted database
Copy and paste the database into the assets folder (you may have to create the assets folder, right click App/New/Directory/src/main/assets)
Create an #Dao annotated interface so that the database can be accessed easily. e.g.
#Dao
interface AllDao {
#Query("SELECT * FROM ${ESTABLISHMENT_TABLE_NAME}")
fun getAllEstablishments(): List<Establishment>
#Query("SELECT * FROM ${CLASSROOM_TABLE_NAME}")
fun getAllClassrooms(): List<Classroom>
#Query("SELECT * FROM ${ESTABLISHMENTCLASSROOMMAP_TABLE_NAME}")
fun getAllEstablishmentClassroomMaps(): List<EstablishmentClassroomMap>
}
Amend the #Database annotated class to include an abstract function to get an instance of the #Dao annotated class. You may wish to add a singleton to allow a single instance of the database to be retrieved rather than using Room's databaseBuilder method multiple times. Noting that you will be using the databaseBuilder's createFromAsset to copy the database from the assets folder when the database is first required (i.e. the very first time the App is run) so you could for example have:-
#Database(entities = [Establishment::class,Classroom::class, EstablishmentClassroomMap::class], exportSchema = false, version = 1)
abstract class TheDatabase: RoomDatabase() {
abstract fun getAllDao(): AllDao
companion object {
private var instance: TheDatabase? = null
fun getInstance(context: Context): TheDatabase {
if (instance == null) {
instance = Room.databaseBuilder(context, TheDatabase::class.java, "TheDatabase.db")
.allowMainThreadQueries()
.createFromAsset("TheDatabase.db")
.build()
}
return instance as TheDatabase
}
}
}
NOTE the string encoded in the createFromAsset this MUST match the file name of the copied database (which can be renamed to suit) e.g:-
Lastly to test some code is added in an activity to access the database (the copy from the assets folder will only be done if an attempt is made to actually access the database (getting an instance of the #Database annotated class WILL NOT access the database)) so for example:-
class MainActivity : AppCompatActivity() {
lateinit var db: TheDatabase
lateinit var dao: AllDao
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
db = TheDatabase.getInstance(this)
dao = db.getAllDao()
for (e in dao.getAllEstablishments()) {
Log.d("DBINFO_ESTAB","ID is ${e.establishment_id} name is ${e.name} etc")
}
for (c in dao.getAllClassrooms()) {
Log.d("DBINFO_CLS","ID is ${c.classroomId} name is ${c.name} etc")
}
for (ecm in dao.getAllEstablishmentClassroomMaps()) {
Log.d("DBINFO_ECM", "ESTID is ${ecm.establishment_id_map} CLSID is ${ecm.classroom_id_map}")
}
}
}
And when run the log contains:-
2022-11-04 14:04:22.436 D/DBINFO_ESTAB: ID is 100 name is Massachusetts Institute of Technology (MIT) etc
2022-11-04 14:04:22.436 D/DBINFO_ESTAB: ID is 110 name is Oxford University etc
2022-11-04 14:04:22.436 D/DBINFO_ESTAB: ID is 120 name is Cambridge University etc
2022-11-04 14:04:22.436 D/DBINFO_ESTAB: ID is 130 name is Hull University etc
2022-11-04 14:04:22.436 D/DBINFO_ESTAB: ID is 131 name is Imperial College etc
2022-11-04 14:04:22.439 D/DBINFO_CLS: ID is 200 name is RM001 etc
2022-11-04 14:04:22.439 D/DBINFO_CLS: ID is 210 name is RM002 etc
2022-11-04 14:04:22.439 D/DBINFO_CLS: ID is 220 name is LAB001 etc
2022-11-04 14:04:22.439 D/DBINFO_CLS: ID is 230 name is LAB002 etc
2022-11-04 14:04:22.441 D/DBINFO_ECM: ESTID is 100 CLSID is 200
2022-11-04 14:04:22.441 D/DBINFO_ECM: ESTID is 100 CLSID is 220
2022-11-04 14:04:22.441 D/DBINFO_ECM: ESTID is 110 CLSID is 210
2022-11-04 14:04:22.441 D/DBINFO_ECM: ESTID is 110 CLSID is 230
2022-11-04 14:04:22.441 D/DBINFO_ECM: ESTID is 120 CLSID is 220
2022-11-04 14:04:22.441 D/DBINFO_ECM: ESTID is 120 CLSID is 230
2022-11-04 14:04:22.441 D/DBINFO_ECM: ESTID is 130 CLSID is 200
2022-11-04 14:04:22.441 D/DBINFO_ECM: ESTID is 130 CLSID is 210
Using App inspection and the query from the conversion:-

How do I prevent sql alchemy from inserting the None value to field?

The Alembic migration script :
def upgrade():
uuid_gen = saexp.text("UUID GENERATE V1MC()")
op.create_table(
'foo',
sa.Column('uuid', UUID, primary_key=True, server_default=uuid_gen),
sa.Column(
'inserted',
sa.DateTime(timezone=True),
server_default=sa.text("not null now()"))
sa.Column('data', sa.Text)
)
This is my Base class for SQL Alchemy:
Class Foo(Base):
__tablename__ = 'foo'
inserted = Column(TIMESTAMP)
uuid = Column(UUID, primary_key=True)
data = Column(TEXT)
It has a static mehtod for insert :
#staticmethod
def insert(session, jsondata):
foo = Foo()
foo.data = jsondata['data']
if 'inserted' in jsondata:
foo.inserted = jsondata['inserted']
if 'uuid' in jsondata:
foo.uuid = jsondata['uuid']
session.add(foo)
return foo
the purpose of the 2 if's are to simplify testing. this way i can "inject" a uuid and inserted date, to get predictible data for my tests
When trying to insert data
foo = Foo()
foo.insert(session, {"data": "foo bar baz"})
session.commit()
I get an IntegrityError :
[SQL: 'INSERT INTO foo (inserted, data) VALUES (%(inserted)s, %(data)s) RETURNING foo.uuid'] [parameters: {'data': 'foo bar baz', 'inserted': None}]
wich seem normal to me because the insert violates the "not-null" constraint in the postgres database.
How do I prevent sql alchemy from inserting the None value to the inserted field ?
While playing and testing around, I found that if the "inserted" column is defined as primary key , sql alchemy does not include the field in the insert statement.
def upgrade():
uuid_gen = saexp.text("UUID GENERATE V1MC()")
op.create_table(
'foo',
sa.Column('uuid', UUID, primary_key=True, server_default=uuid_gen),
sa.Column(
'inserted',
primary_key=True,
sa.DateTime(timezone=True),
server_default=sa.text("not null now()"))
sa.Column('data', sa.Text)
)
But this is not what I want.
The primary problem is the server_default which is missing in the inserted member in class Foo. It's only present in the alembic script. Note that the alembic definitions are only used when running the migrations. They do not affect the application. For this reason, it's a good idea to copy the exact same definitions from the alembic script to your application (or vice-versa).
Because no value is defined in the model definition, sqlalchemy seems to set this to None when the class is instantiated. This will then be sent to the DB which will complain. To fix this, either set default or server_default on the model definition (the class inheriting from Base).
Some additional notes/questions:
Where does UUID GENERATE V1MC() come from? The official docs look different. I replaced it with func.uuid_generate_v1mc().
The server_default value in your case contains not null which is incorrect. You should set nullable=False on you column attribute (see below).
alembic script
# revision identifiers, used by Alembic.
revision = THIS_IS_DIFFERENT_ON_EACH_INSTANCE! # '1b7e145f2138'
down_revision = None
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID
def upgrade():
op.create_table(
'foo',
sa.Column('uuid', UUID, primary_key=True,
server_default=sa.func.uuid_generate_v1mc()),
sa.Column(
'inserted',
sa.DateTime(timezone=True),
nullable=False,
server_default=sa.text("now()")),
sa.Column('data', sa.Text)
)
def downgrade():
op.drop_table('foo')
tester.py
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, create_engine, func
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.dialects.postgresql import (
TEXT,
TIMESTAMP,
UUID,
)
engine = create_engine('postgresql://michel#/michel')
Session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
class Foo(Base):
__tablename__ = 'foo'
inserted = Column(TIMESTAMP, nullable=False,
server_default=func.now())
uuid = Column(UUID, primary_key=True,
server_default=func.uuid_generate_v1mc()),
data = Column(TEXT)
#staticmethod
def insert(session, jsondata):
foo = Foo()
foo.data = jsondata['data']
if 'inserted' in jsondata:
foo.inserted = jsondata['inserted']
if 'uuid' in jsondata:
foo.uuid = jsondata['uuid']
session.add(foo)
return foo
if __name__ == '__main__':
session = Session()
Foo.insert(session, {"data": "foo bar baz"})
session.commit()
session.close()
output after execution
[9:43:54] michel#BBS-nexus [1 background job(s)]
/home/users/michel/tmp› psql -c "select * from foo"
uuid | inserted | data
--------------------------------------+-------------------------------+-------------
71f5fd32-0602-11e6-aebb-27be4bbac26e | 2016-04-19 09:43:45.297191+02 | foo bar baz
(1 row)

Composite Key and mapping in grails 2.2.5 with legacy database

I have 4 tables. osiguranje_paket, atribut, tip_unosa, razna_polja. osiguranje_paket, atribut, tip_unosa are Parents of razna_polja table.
razna_polja table has composite key that is consisted from two primary keys (osgp_id = osiguranje_paket table + atr_id = atribut table). The relationships between them are one-to-many bidirectional and I'm using Legacy PostgreSQL Database with dynamic scaffolding, I can not make any changes to database or tables or anything. How can I map my classes to use composite key, what do I need to add or change in my domains? Any help would be appreciated.
CREATE TABLE revoco.osiguranje_paket
(
osgp_id serial NOT NULL,
osg_id integer NOT NULL,
osgp_napomena character varying(500),
tpo_id integer NOT NULL,
osgp_link character varying(155),
osgp_oznaka character varying(10),
CONSTRAINT osgp_pk PRIMARY KEY (osgp_id),
CONSTRAINT osg_osgp_fk FOREIGN KEY (osg_id)
REFERENCES revoco.osiguranje (osg_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT tpo_osgp_fk FOREIGN KEY (tpo_id)
REFERENCES revoco.tip_osiguranja (tpo_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
CREATE TABLE revoco.atribut
(
atr_id serial NOT NULL,
atr_naziv character varying(155) NOT NULL,
lab_id integer,
atr_rbr integer,
CONSTRAINT atr_pk PRIMARY KEY (atr_id),
CONSTRAINT atr_lab_labela_fk FOREIGN KEY (lab_id)
REFERENCES common.labela (lab_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
CREATE TABLE common.tip_unosa
(
tpu_id serial NOT NULL,
tpu_val character varying(32) NOT NULL,
CONSTRAINT tpu_pk PRIMARY KEY (tpu_id),
CONSTRAINT tpu_vrijednost_unique UNIQUE (tpu_val)
)
CREATE TABLE common.razna_polja
(
osgp_id integer NOT NULL,
atr_id integer NOT NULL,
tpu_id integer NOT NULL,
rap_odjel integer NOT NULL DEFAULT 0,
rap_vidljiv boolean NOT NULL DEFAULT true,
CONSTRAINT rap_pk PRIMARY KEY (osgp_id, atr_id),
CONSTRAINT rap_atr_atribut_fk FOREIGN KEY (atr_id)
REFERENCES revoco.atribut (atr_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT rap_osgp_paket_fk FOREIGN KEY (osgp_id)
REFERENCES revoco.osiguranje_paket (osgp_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT rap_tpu_tip_unosa_fk FOREIGN KEY (tpu_id)
REFERENCES common.tip_unosa (tpu_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT rap_ispravan_odjel_ck CHECK (rap_odjel >= 0 AND rap_odjel <= 1)
)
This are my Domain classes
OsiguranjePaket.groovy
import common.RaznaPolja
class OsiguranjePaket {
Integer id
String osgp_napomena
String osgp_link
String osgp_oznaka
static belongsTo = [osg: Osiguranje, tpo: TipOsiguranja]
static hasMany = [raznaPolja: RaznaPolja]
String toString(){
"${osgp_oznaka}"
}
static fetchMode = [raznapolja: 'eager']
static constraints = {
id(unique: true)
osgp_link (nullable: true, blank: false, size: 0..155)
osgp_napomena (nullable: true, blank: false, size: 0..500)
osgp_oznaka (nullable: true, blank: false, size: 0..10)
}
static mapping = {
table name: 'osiguranje_paket', schema: 'revoco'
version false
id generator :'identity', column :'osgp_id', type:'integer'
}
}
Atribut.groovy
import common.RaznaPolja
import common.Labela
class Atribut {
Integer id
String atr_naziv
Integer atr_rbr
static hasMany = [raznaPolja: RaznaPolja]
static belongsTo = [lab: Labela]
static fetchMode = [raznaPolja: 'eager']
String toString(){
"${atr_naziv}"
}
static mapping = {
table name: "atribut", schema: "revoco"
version false
id generator :'native', column :'atr_id'
}
static constraints = {
id(blank: false, unique: true)
atr_naziv (blank: false, size: 0..155)
atr_rbr (nullable: true)
}
}
TipUnosa.groovy
class TipUnosa {
Integer id
String tpu_val
static hasMany = [raznaPolja: RaznaPolja]
static fetchMode = [raznaPolja: 'eager']
String toString(){
"${tpu_val}"
}
static constraints = {
id (blank:false, size: 0..10)
tpu_val (blank:false, unique:true, size:0..32)
}
static mapping = {
table name: "tip_unosa", schema: "common"
version false
id generator :'identity', column :'tpu_id', type:'integer'
}
}
RaznaPolja.groovy
import java.io.Serializable;
import revoco.Atribut
import revoco.OsiguranjePaket
class RaznaPolja implements Serializable {
Integer rap_odjel
Boolean rap_vidljiv
//without this getting common.RaznaPolja(unsaved)
String toString(){
"${id}" //Getting null
}
static belongsTo = [atr: Atribut, osgp: OsiguranjePaket, tpu: TipUnosa]
static mapping = {
table name: 'razna_polja', schema: 'common'
id composite: ['osgp', 'atr']
// cache usage:'read-only'
version false
rap_odjel column: 'rap_odjel', type: 'integer'
rap_vidljiv column:'rap_vidljiv', type: 'boolean'
}
}
Composite key
To set a composite key you need to use the mapping property of your domain class and your domain class must implement the Serializable interface. Here's an example from the Grails documentation.
import org.apache.commons.lang.builder.HashCodeBuilder
class Person implements Serializable {
String firstName
String lastName
boolean equals(other) {
if (!(other instanceof Person)) {
return false
}
other.firstName == firstName && other.lastName == lastName
}
int hashCode() {
def builder = new HashCodeBuilder()
builder.append firstName
builder.append lastName
builder.toHashCode()
}
static mapping = {
id composite: ['firstName', 'lastName']
}
}
Database mapping
The domain class mapping property is also used to change the database table and columns your domain class maps to as you can see here.
Associations
As for the relationships between the tables, the documentation can give you some clues. You may have to add some mapping domain classes here and there to create what you need, but the Grails associations should be able to handle your needs.

Why am I getting "Foreign key constraint fails" exception on persist?

Good day, please advise why am I getting following exception. I'm EclipseLink beginner, I'm using jdk1.7.0_05, MySQL server 5.5.25a.
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`stats`.`version`, CONSTRAINT `fk_version_activity1` FOREIGN KEY (`activity_id`, `activity_license_id`) REFERENCES `activity` (`id`, `license_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Error Code: 1452
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`stats`.`version`, CONSTRAINT `fk_version_activity1` FOREIGN KEY (`activity_id`, `activity_license_id`) REFERENCES `activity` (`id`, `license_id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Error Code: 1452
Call: INSERT INTO version (version, activity_id, product, activity_license_id) VALUES (?, ?, ?, ?)
bind => [4 parameters bound]
Query: InsertObjectQuery(cz.ryvo.stats.database.Version[ versionPK=cz.ryvo.stats.database.VersionPK[ product=AP, activityId=0, activityLicenseId=0 ] ])
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:786)
at cz.audatex.audaupdateloader.DatabaseHelper.registerActivity(DatabaseHelper.java:104)
... 3 more
Table License looks like this:
CREATE TABLE `license` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`license` varchar(20) NOT NULL,
`organisation_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `license_UNIQUE` (`license`),
KEY `fk_license_user1` (`user_id`),
KEY `fk_license_organisation1` (`organisation_id`),
CONSTRAINT `fk_license_organisation1` FOREIGN KEY (`organisation_id`) REFERENCES `organisation` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_license_user1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
Table Activity:
CREATE TABLE `activity` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`license_id` int(11) NOT NULL,
`time` datetime NOT NULL,
`type` char(1) NOT NULL,
`result` int(1) DEFAULT NULL,
PRIMARY KEY (`id`,`license_id`),
UNIQUE KEY `activity_UQ01` (`license_id`,`time`,`type`),
KEY `fk_activity_license1` (`license_id`),
CONSTRAINT `fk_activity_license1` FOREIGN KEY (`license_id`) REFERENCES `license` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
And table Versions:
CREATE TABLE `version` (
`product` varchar(45) NOT NULL,
`activity_id` int(11) NOT NULL,
`activity_license_id` int(11) NOT NULL,
`version` varchar(45) DEFAULT NULL,
PRIMARY KEY (`product`,`activity_id`,`activity_license_id`),
KEY `fk_version_activity1` (`activity_id`,`activity_license_id`),
CONSTRAINT `fk_version_activity1` FOREIGN KEY (`activity_id`, `activity_license_id`) REFERENCES `activity` (`id`, `license_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
My classes I generated in NetBeans using New->Entity Classes from database look like this:
Class Version:
#Entity
#Table(name = "version")
#XmlRootElement
public class Version implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
protected VersionPK versionPK;
#Column(name = "version")
private String version;
#JoinColumns({
#JoinColumn(name = "activity_id", referencedColumnName = "id", insertable = false, updatable = false),
#JoinColumn(name = "activity_license_id", referencedColumnName = "license_id", insertable = false, updatable = false)})
#ManyToOne(optional = false, fetch = FetchType.LAZY)
private Activity activity;
...
Class VersionPK:
#Embeddable
public class VersionPK implements Serializable {
#Basic(optional = false)
#Column(name = "product")
private String product;
#Basic(optional = false)
#Column(name = "activity_id")
private int activityId;
#Basic(optional = false)
#Column(name = "activity_license_id")
private int activityLicenseId;
...
I'm gettin the exception when I execute following code:
...
ActivityPK apk = new ActivityPK();
apk.setLicenseId(activity.getLicense().getId());
activity.setActivityPK(apk);
em.persist(activity);
em.flush(); <- Here the exception is thrown
...
I suspect it is because activity does not have ID assigned yet and VerionPK uses this ID. Am I right? What is the proper way to persist such data? Should I persist version collection separately after persisting activity with version collection set to null?
Many thanks in advance. Vojtech
Check out JPA 2.0's derived Ids. You can mark the relationship as the ID, or specify that the relationship the id field using maps Id instead of having to manually set the value yourself. This takes care of the problem when creating a new tree and the root entity uses sequencing - the ids aren't available to the children to use as foreign keys until the root is persisted.
Otherwise, the root entity needs to be persisted and flushed so that an Id is assigned. In this case, the Activity entity needs primary key values before you can try and persist a new Version object that reference it since you must manually set all the version.versionpk values.

How to map inheritance discriminator as composite key in Entity Framework?

Is it possible to map a one to one relationship using the parent key and a discriminator value? I know that code first does not like the discriminator property on the concrete class and to reference it only in the Map method.
FlightTypes { Inbound = 1, Outbound = 2}
public class Transaction
- int TransactionId
- int? InboundFlightId
- InboundTransactionFlight InboundFlight
- int? OutboundFlightId
- OutboundTransactionFlight OutboundFlight
public abstract class TransactionFlight
- TransactionFlightId
public class InboundTransactionFlight : Flight
- List<Transaction> InboundFor
public class OutboundTransactionFlight : Flight
- List<Transaction> OutboundFor
Entity<InboundTransactionFlight>().Map(m => m.Requires("FlightTypeId").HasValue(1));
Entity<OutboundTransactionFlight>().Map(m => m.Requires("FlightTypeId").HasValue(2));
/* this is what is currently generated */
CREATE TABLE Transactions (
TransactionId int NOT NULL,
InboundFlightId int NULL,
OutboundFlightId int NULL
)
CREATE TABLE TransactionFlights (
TransactionFlightId int NOT NULL,
FlightTypeId int NOT NULL,
...
CONSTRAINT PK_TransactionFlights PRIMARY KEY CLUSTERED ( TransactionFlightId )
)
/* is it possible to generate/map this and keep inheritance? */
CREATE TABLE Transactions (
TransactionId int NOT NULL,
)
CREATE TABLE TransactionFlights (
TransactionId int NOT NULL,
FlightTypeId int NOT NULL,
...
CONSTRAINT PK_TransactionFlights PRIMARY KEY CLUSTERED ( TransactionId, FlightTypeId )
)
Thanks.
As I know it is not possible because EF doesn't allow using discriminator column in any other mapping. Also your target mapping will demand that your transaction class also has FlightTypeId property (class must have properties for the whole key) but it would break the meaning of the inheritance because you would be able to change the value of that property and make your inheritance inconsistent.