views:

43

answers:

2

Hi all,

I recently added new property in one class, its list property and I have written xml mapping for that property,due to few reasons I am not suppose to delete database or use create option in hibernate config file to update changes. I have to do it manual by executing sql queries on database.

xml mapping file:

<list name="items" table="ITEM_ITEM_GROUP" lazy="false" cascade="save-update">
    <key column="ITEM_GROUP_ID"></key>
    <list-index column="IG_INDEX" />            
    <many-to-many column="ITEM_ID" class="Item" />
</list>

can anyone please help how do i do it?

+1  A: 

So you just want to add that column to the table? Use the ALTER TABLE statement:

ALTER TABLE table_name
ADD column_name datatype

See http://w3schools.com/sql/sql_alter.asp

Ben J
True for "regular" properties, but the many-to-many list is not a column, it is a link table.
Stefan Steinegger
but I don't think that is solution for property type collection. what is column name from above XML statements. problem is how to set relation between two table.
kumar kasimala
My apologies, I should've paid closer attention to the mapping that was given.
Ben J
+1  A: 

You are not just adding a column, you are adding a table.

The easiest approach is to use SchemaExport to create an sql script of your database schema. Then you can copy-paste the new table with indexes and everything.

In this case, you most probably don't have to change an existing table, just add the new stuff.

In java, you can call SchemaExport directly from the command line. Take a look at a tutorial like this here.

In C# you need to write a utility which export the schema, code looks something like this:

var schemaExport = new SchemaExport(configuration);
schemaExport                    
  .Execute(
    false,
    false,
    false,
    null,
    fileStream);

It will basically be something like this (depending on your rdms you are using)

create table ITEM_ITEM_GROUP(
  ITEM_GROUP_ID int not null,
  IG_INDEX int not null,
  ITEM_ID int,
  primary key (ITEM_GROUP_ID, IG_INDEX)
)

alter table ITEM_ITEM_GROUP
    add constraint FKXXXX
    foreign key (ITEM_GROUP_ID) 
    references YOUR_ENTITY

alter table ITEM_ITEM_GROUP
    add constraint FKXXXX
    foreign key (ITEM_ID) 
    references ITEM

I probably missed something. So it's best you let Hibernate create the schema and look into the generated SQL.

Stefan Steinegger
syntax that you given above is a bit confusing. may I know in which lanuge you have written ? is java,
kumar kasimala
oh sorry, this is C#. It may look similar in java, I fix it.
Stefan Steinegger