Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create table column in a particular order in hibernate? [duplicate]

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?

like image 620
Sandeep P Pillai Avatar asked Oct 26 '17 04:10

Sandeep P Pillai


2 Answers

Use @EmbeddedId

 @Entity
    class Student{
        @EmbeddedId
        StudentInfo id;

        String Name;
        String Class;
        Integer RollNo;
    }

    @Embeddable
    class StudentInfo implements serializable {
        Integer StudentID;
        Integer StudentRowNo;
    }
like image 115
Sandeep Avatar answered Sep 30 '22 15:09

Sandeep


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!

like image 36
N00b Pr0grammer Avatar answered Sep 30 '22 15:09

N00b Pr0grammer