5

There is a duplication for my question but not yet answered. I'm a beginner to hibernate, while creating table automatically from entity in SQL Server using the property

<property name="hibernate.hbm2ddl.auto">create</property>

it seems the order of table column is not correct that was not an issue for me until i used composite key. Now the issue is the order of the column is not as same the business entity. Here is the business entity i created

@Entity
public class SalesEstimateDtl implements Serializable {
@Id
private Long LedSalesEstID;
@Id
private Integer LedSalesEstRowNo;

and here is the generated query

CREATE TABLE [dbo].[SalesEstimateDtl](
             [LedSalesEstRowNo] [int] NOT NULL,
             [LedSalesEstID] [numeric](19, 0) NOT NULL,
             PRIMARY KEY CLUSTERED 
             (
               [LedSalesEstRowNo] ASC,
               [LedSalesEstID] ASC
             ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

How can i change the order of LedSalesEstRowNo with LedSalesEstID?

Sandeep P Pillai
  • 753
  • 1
  • 4
  • 22

3 Answers3

3

Use @EmbeddedId

 @Entity
    class Student{
        @EmbeddedId
        StudentInfo id;

        String Name;
        String Class;
        Integer RollNo;
    }

    @Embeddable
    class StudentInfo implements serializable {
        Integer StudentID;
        Integer StudentRowNo;
    }
Sandeep
  • 413
  • 4
  • 13
2

As a matter of fact, the table created with the columns as specified by your annotations shouldn't be dependent on the sequence of it. Neither a SELECT * statement would entirely ensure the columns in a specific order as well.

And coming to a response from the Hibernate team, it looks like to be a Known issue and it is not possible to set the order of the columns through Hibernate. Post here.

To my understanding, we may not entirely rely on Hibernate's hbm2ddl on creating tables. The moment a table is created via Hibernate, we can very well ALTER it to order the columns as needed.

Hope this answers your question!

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
0

Use Below Property inside Hibernate Config file

<property name="hibernate.hbm2ddl.auto">showShemaUpdate</property>

Then Use below code before creating SessionFactory. String file = "SqlSchema.sql"; SchemaUpdate update = new SchemaUpdate(configuration); update.setOutputFile(file); update.execute(false, false);

It will create SqlSchema.sql file in one location which contains all the DDL Queries.Edit them with whatever order you want.

atul_java
  • 16
  • 1