3,Mapping column named 'interface' in Grails seems impossible - grails

It seems that it is impossible with Grails to map a domain class to a database table that has is a foreign key column named 'interface'.
In my case there's a relationship with two tables INTERFACE and INTERFACE_DETAILS. They are legacy databases and column names can not be changed or added.
Put it simple INTERFACE_DETAILS has an FK column 'INTERFACE' referring to PK INTERFACE.ID
class Interface {
String id;
static mapping = {
table "INTERFACE"
version false
id generator: 'assigned'
id column: 'id', sqlType:"varchar2(20)"
}
class InterfaceDetails {
Interface iface;
static belongsTo = [iface: Interface]
static mapping = {
table "INTERFACE_DETAILS"
version false
id column: 'interface'
iface column: 'INTERFACE', sqlType:"varchar2(20)", insertable: false, updateable: false
}
}
I'm currently using H2 database. When I try to add a row to InterfaceDetails this error occurs:
Referential integrity constraint violation: "FK351396597E3917F9: PUBLIC.INTERFACE_DETAILS FOREIGN KEY(INTERFACE) REFERENCES PUBLIC.INTERFACE(ID)"; SQL statement:
insert into INTERFACE_DETAILS (interface) values (null) [23506-164]
I wonder why hibernate adds null for 'interface' value ?
I do have to have line : "id column: 'interface'" because in other case Hibernate will generate an extra column: "iface_id" and this is not suitable. Existing database columns can not be changed.
This is quite puzzling. Please tell me that Grails can handle this situation.
stripped down schema:
create table interface (id varchar2(20) not null,primary key (id));
create table interface_details (interface varchar2(20) not null, name varchar(255) not null, primary key (interface));
alter table interface_details add constraint FK_INTERFACE foreign key (interface) references interface;

Related

Making a one to many relationship on one table GRAILS

I have the parent domain of Teacher and child domain of Student ( one to many)
Student have entity of student_certificate which is a byte( upload file to be exact)
my concern here is i want to separate the student_certificate and create another domain of Student_attachment, but the twist i want to do is to lies the student_attachment on the table of Student
is it possible to do?because there is an existing data so creating another table is a risky way
...but the twist i want to do is to lies the student_attachment on the
table of Student
You can use the embedded attribute as shown below:
class Teacher {
String name
static hasMany = [students: Student]
}
class Student {
String name
StudentAttachment certificate
static embedded = ['certificate']
}
class StudentAttachment {
byte[] attachment
}
create table student (id bigint generated by default as identity, version bigint not null, name varchar(255) not null, certificate_attachment binary(255) not null, primary key (id));
create table teacher (id bigint generated by default as identity, version bigint not null, name varchar(255) not null, primary key (id));
create table teacher_student (teacher_students_id bigint not null, student_id bigint);

Change foreign key type without losing data.(asp.net)

I have created one to many relationship between student and department tables using entity framework.But unfortunately in the student table the foreign key (DepartmentId) type I gave string but it should be int.How can i resolve this issue without losing data????????
N.B: I am using entity framework code first approach.
I think you cannot do that, considering that your DepartamentID is the PK of the table Departament, so you have to drop the old field and create a new one, something like this
AddColumn("dbo.Departament", "DepartamentID2", c => c.Int(nullable: false));
Sql(#"
UPDATE dbp.Departament
SET DepartamentID2 = DepartamentID
");
Sql(#"
ALTER TABLE dbo.Departament drop CONSTRAINT <The FK Constraint>
");
DropColumn("dbo.Departament", "DepartamentID");
RenameColumn("dbo.Departament", "DepartamentID2", "DepartamentID");
Sql(#"
ALTER TABLE dbo.Departament add PRIMARY KEY (DepartamentID)
");
Sql(#"
ALTER TABLE dbo.Departament add CONSTRAINT <The FK
constraint>
");

GORM creating tables with base class name instead concrete class name

I Have the following domains in my gorm package:
Domain.groovy
package gorm
class Domain {
String createdBy
static constraints = {
}
static mapping = {
tablePerHierarchy true
}
}
User.groovy
package gorm
class User extends Domain {
String name
static constraints = {
}
}
I want a table named user with the fields of the base class domain, but instead GROM generate a table whit this specification
create table domain
(
id bigint auto_increment
primary key,
version bigint not null,
created_by varchar(255) not null,
class varchar(255) not null,
name varchar(255) null
)
I'm using mysql driver with grails 2.5.6.
It generates the table with the name as domain because you are using tablePerHierarchy true in your mapping.
Basically, you will get one table where you can set different discriminators for the subclasses.
See here for more information on inheritance strategies: http://docs.grails.org/2.5.x/guide/single.html#GORM
(scroll down to: 7.2.3 Inheritance in GORM)
If you simply want schema-export to generate your table with the name as user, then you would need to add the following to your mapping block in the Domain class:
table 'user'
so the entire mapping block would look like:
static mapping = {
table 'user'
tablePerHierarchy true
}
However, this may not make sense to name the table user if you have other classes extend from Domain.
(and if you don't plan to have other classes extend from Domain, then just add your fields into your User domain).
If you want to generate two tables (Domain and User), then set tablePerHierachy false.
Here is a great write-up with examples that may help you decide which way you want to go for your project:
https://sysgears.com/articles/advanced-gorm-features-inheritance-embedded-data-maps-and-lists-storing/
As a side note: I'm not keen on the name Domain for a domain class; it is too generic and may get confusing when you are talking about the specific Domain class vs domain classes. At least name it BaseDomain.

GORM column mapping not respecting underscore before number

I have a column in my table called HDR_PROSHIP_REFERENCE_1 and a mapping in the domain of:
static mapping = {
table 'ORDER_LINE'
version false
id column: 'LINE_ID', insertable: 'false', updateable: 'false'
proshipReference1 column: 'HDR_PROSHIP_REFERENCE_1'
proshipReference2 column: 'HDR_PROSHIP_REFERENCE_2'
}
When GORM goes to build out a query against this, however, SQL throws an invalid identifier:
ORA-00904: "THIS_"."HDR_PROSHIP_REFERENCE2": invalid identifier
It looks like GORM, in its effort to camelcase columns by default, is not respecting the '_' before the numbers.
Is this a bug with GORM or am I not writing the mappings correctly?

How to implements tablePerHierarchy with disciminator in grails?

I have a class hirarchy :
class Item {}
class Participation extends Item{}
class Contribution extends Participation{}
class Question extends Participation{}
I would like to have a table per class so, I add tablePerHierarchy false in Item
I need a discrimator to implements a query : where class = "Contribution"
I try a lot of implementation but it's not working.
How to do that ?
Thanks
Do you want table per hierarchy or table per class? It's not clear in your question.
With the following domain objects, you can do it either way:
// Item.groovy
class Item {
String x
}
// Participation.groovy
class Participation extends Item {
String y
}
Using the default, table per hierarchy strategy, just one table will be used to store Items and all the subclasses of Items too. The default discriminator column is called class, which grails will use automatically. The schema generated by grails schema-export looks like this:
create table item (
id bigint generated by default as identity (start with 1),
version bigint not null,
x varchar(255) not null,
class varchar(255) not null,
y varchar(255),
primary key (id)
);
There's just one table for both classes which contains all the fields declared in every class in the hierarchy plus the discriminator column class. If you do a query like Participation.list(), the SQL grails generates looks like this:
select
this_.id as id1_0_,
this_.version as version1_0_,
this_.x as x1_0_,
this_.y as y1_0_
from
item this_
where
this_.class='Participation'
By changing the inheritance strategy to table per class with static mapping { tablePerHieracrchy false } in Item.groovy, grails will generate a table for each of classes in the hierarchy. Each table stores only the fields declared in each class, so a Participation object would be represented by a row in both the Item table and the Participation table. The schema looks like this:
create table item (
id bigint generated by default as identity (start with 1),
version bigint not null,
x varchar(255) not null,
primary key (id)
);
create table participation (
id bigint not null,
y varchar(255) not null,
primary key (id)
);
And the SQL for Participation.list() changes to:
select
this_.id as id1_0_,
this_1_.version as version1_0_,
this_1_.x as x1_0_,
this_.y as y2_0_
from
participation this_
inner join
item this_1_
on this_.id=this_1_.id

Resources