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?
Use @EmbeddedId
@Entity
class Student{
@EmbeddedId
StudentInfo id;
String Name;
String Class;
Integer RollNo;
}
@Embeddable
class StudentInfo implements serializable {
Integer StudentID;
Integer StudentRowNo;
}
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With