I'm new to NHibernate, so this is probably my mistake, but when I use:
schema.Create(true, true);
I get:
SchemaExport [(null)]- There is already an object named 'XXX' in the database.
System.Data.SqlClient.SqlException: There is already an object
named 'XXX' in the database.
I grabbed the SQL code nHibernate was using, ran it directly from MSSMS, and recieved similar errors. Looking into it, the generated code is not properly dropping the foreign key constraints. The drop looks like this:
if exists (select 1 from sysobjects where id = OBJECT_ID(N'dbo[FK22212EAFBFE4C58]')
AND parent_obj = OBJECT_ID('YYY'))
alter table dbo.YYY drop constraint FK22212EAFBFE4C58
Doing a "select OBJECT_ID(N'dbo[FK22212EAFBFE4C58]')" I get null. If I take out the "dbo" (i.e. "select OBJECT_ID(N'[FK22212EAFBFE4C58]')") then the ID is returned.
So, my question is, why is nHibernate adding the dbo, and why does that prevent the object from being returned (since the table owning the constraint is dbo.XXX)
One of my mapping files:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping namespace="CanineApp.Model" assembly="CanineApp.Model" xmlns="urn:nhibernate-mapping-2.2">
<class name="MedicalLog" table="MedicalLog" schema="dbo">
<id name="MedicalLogID" type="Int64">
<generator class="identity" />
</id>
<property name="InvoiceAmount" type="Decimal" not-null="true" />
...
<many-to-one name="Canine" class="Canine" column="CanineID" not-null="true" fetch="join" />
<many-to-one name="TreatmentCategory" class="TreatmentCategory" column="TreatmentCategoryID" not-null="true" access="field.camelcase-underscore" />
</class>
</hibernate-mapping>
Answer: .
It's probably a bug. There was an entry for this exact issue for SQL Server 2005. It doesn't appear to have been flagged for SQL 2000, so I'll either create a bug report or fix it.
Related
I am trying to populate 2 tables through csv files in liquibase.
I have one table called tenant and one another named tenant_configuration and have foreign key to tenant. First part is load tenant data:
<changeSet id="1" context="test">
<comment>Insert data for tenant table</comment>
<loadUpdateData
primaryKey="id"
file="tenant.csv"
tableName="tenant"/>
</changeSet>
Then i would like to use another csv file to populate tenant config but retrieve tenant_id from first change.
<changeSet id="2" context="test">
<comment>Insert data for tenant_db_configuration table</comment>
<loadData tableName="tenant_db_configuration"
file="tenant_db_configuration.csv"
separator="," >
<column name="tenant_id" type="NUMERIC" defaultValueComputed="(SELECT ID FROM tenant WHERE tenant_id = tenant_1)"/>
<column header="username" name="username" type="STRING"/>
<column header="password" name="password" type="STRING"/>
</loadData>
</changeSet>
Tried this but liquibase ignore the tenant_id part and shows:
[Failed SQL: INSERT INTO [dbo].[tenant_db_configuration] ([username], [password]) VALUES...
how i can retrieve that foreign key and merge with existing csv file to load data?
thanks!
As far as i know you will need to make the relation manually in the secomnd CSV file, tenant_db_configuration.csv. I mean, each row in there will need to be pointing to an existent id in the tenat Table.
If the order doesnt matters bc you trust in your csv data, You could disable the foreign key checks with a changeSet before starting to import the data.
I dunno wich DBMS are you using but with MYSQL is
<changeSet author="liquibase-docs" id="sql-example">
<sql dbms="mysql">
SET FOREIGN_KEY_CHECKS=0;
</sql>
</changeSet>
and then you can enable it after on a separate changeSet.
It's been 3 months since the question, so Hope it helps. If you solved it already, what was the solution?
I want to add check constraint to BLOB type column which stores JSON data, in CREATE Table script in liquibase(version 3.3.5, database -Oracle 12C). but it does not compile. Can anyone please explain what is the right syntax to add constraint which ensures only JSON type data would be inserted. I followed this question
Plain sql : CONSTRAINT ensure_json CHECK (po_document IS JSON))
But not sure what is liquibase equivalent for this.
PostgreSQL Check Constraint in Liquibase
<changeSet id="Change_id" author="xqz">
<createTable tableName="table_name">
<column name="pkey" type="int">
<constraints primaryKey="true"/>
</column>
<column name="table2_pkey" type="int">
<constraints nullable="false"/>
</column>
<column name="name" type="varchar(100)">
<constraints nullable="false"/>
</column>
<column name="filters" type="BLOB">
<constraints checkConstraint="ensure_json CHECK (filters IS JSON)" />
</column>
</createTable>
</changeSet>
If I add constraint to filters column, build fails, If I remove it, build is successful. What am I doing wrong. I could not find syntax for it in liquibase docs.
You cannot define check constraints in liquibase, for aditional information see this forum entry.
You'll have to use an <sql> tag like
<sql dbms=oracle>
CREATE TABLE table_name (
pkey integer PRIMARY KEY,
table2_pkey integer NOT NULL,
name varchar(100) NOT NULL,
flter blob CONSTRAINT ensure_json CHECK (filters IS JSON)
)
</sql>
Should work just the same, except that you have to add your own <rollback> tag.
I would like to do a liquibase insert with the primary key being auto generated from the sequence defined in the database. The target database is HSQLDB.
It works to do an insert specifying a value for the primary key
<insert ...>
<column name="TAG_ID" valueNumeric="2"/>
I found this (admittedly older) conversation about it but the issue is still the same. The suggested fix doesn't work for HSQLDB.
Looking at the docs I've tried some things like
<column name="TAG_ID" defaultValueSequenceNext="TAG_ID_SEQ" />
<column name="TAG_ID" defaultValueSequenceNext="TAG_ID_SEQ.NEXTVAL" />
<column name="TAG_ID" valueComputed="TAG_ID_SEQ.NEXTVAL" />
<column name="TAG_ID" autoIncrement="true" />
but none of those put anything in the key when I do the insert (the insert fails on a null primary key).
How does one accomplish this?
HSQLDB has a setting to use Oracle syntax. You can set HSQLDB to use oracle syntax like so:
<changeSet ...
<sql dbms="hsqldb" >SET DATABASE SQL SYNTAX ORA TRUE</sql>
</changeSet>
After that, it works to do the insert like this:
<insert ...
<column name="TAG_ID" valueComputed="TAG_ID_SEQ.NEXTVAL"/>
If one would want to insert into a view, what would be required to setup on NHibernate to allow this.
Neither
<generator class="identity" />
nor
<generator class="native" />
allows insert.
Error I get when I try this is either "Null id" or "Null identifier".
You can not insert, update or delete from a view even with regular SQL commands, nor with NHiberanate. Views are designed for read only purposes.
You should model your class from the original tables in a similar fashion as the view and then you could do any CRUD operation on it.
I have never done this but have you tried assigned:-
<generator class="assigned" />
Any modifications, including UPDATE, INSERT, and DELETE statements,
must reference columns from only one base table.
See here MSDN for more info
And there is one more thing if you use assigned, you have to explicitly specify to NHibernate if the object should be saved or updated by calling either the Save() or Update() method of the ISession.
I need to update a joined sub-class. Since Hibernate doesn't allow to update joined sub-classes in hql or named-query I want to do it via SQL. I also can't use a sql named-query because updates via named-query are not supported in Hibernate.
So I decided to use a SQLQuery. But Hibernate complaints about not calling addScalar():
org.hibernate.QueryException: addEntity() or addScalar() must be called on a sql query before executing the query.
Are updates returning the number of rows affected and how is named that column?
Are there any other ways to do an update on a joined sub-class in hibernate?
Here is a brief example of what i'm trying to do:
<hibernate-mapping>
<class name="example.NiceClass" table="NICE_CLASS">
<meta attribute="class-code"></meta>
<id name="id" type="java.lang.Long">
<column name="NICE_CLASS_ID" precision="8" />
<generator class="sequence">
<param name="sequence">NICE_CLASS_SEQ</param>
</generator>
</id>
</class>
</hibernate-mapping>
<hibernate-mapping package="example">
<joined-subclass name="SubJoinedClass" table="SUB_JOINED_CLASS"
extends="NiceClass">
<key column="NICE_CLASS_ID" foreign-key="NICE_JOINED_ID_FK"/>
<property name="name" type="string" not-null="false">
<column name="NAME" >
<comment>name</comment>
</column>
</property>
</joined-subclass>
</hibernate-mapping>
Thanks in advance!
So I want to do a:
update SubJoinedClass set name = 'newName'
How do you execute the update query ? What method do you call to execute it ? Do you call 'ExecuteUpdate' ?
And why do you think that you cannot execute an update query on a subclass using HQL ? AFAIK, it is possible.