-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: SchemaExport export DDL fields ordered by field name
PostPosted: Sun Nov 29, 2015 9:07 pm 
Newbie

Joined: Sun Nov 23, 2003 4:34 am
Posts: 6
Is there anyway that make SchemaExport export fields according to order of fields?

I am moveing oracle to PostgreSQL.

I create entities(POJO) by Oracle Tables and It is fine.
It create entity fields as same order in oracle table.

When I create tables in PostgreSQL with <property name="hibernate.hbm2ddl.auto" value="create"/>,
The order of Fields is changed with readjusted by field name.

ex)
in oracle table

create table table_name (
id Number(10),
field2 varchar(20).
field1 varchar(50)
);

then
public class TableName implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "ID")

private BigDecimal id;
@Column(name = "field2")
private String field2;
@Column(name = "field1")
private String field1;
......
}

created table in PostgreSQL

CREATE TABLE cdt_hs_unit
(
id numeric(10) NOT NULL,
field1 character varying(50),
field2 character varying(20)
......
)

summary
When oreginal field order is "id, field2, field1" SchemaExport export changes field order by sorting by field name
such as "id, field1, field2".

So this make me d​ifficult to move data because I have to rearrange the order.


Top
 Profile  
 
 Post subject: Re: SchemaExport export DDL fields ordered by field name
PostPosted: Mon Nov 30, 2015 2:29 am 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
Hi jaejong

Unfortunately, hibernate can't reach your goal.

(1) Reason:
The reason is Java Reflection API iterators the members of class with unpredictable order
Code:
// Iterates members with unpredictable order.
for (Field field : YourType.class.getDeclaredFields()) {
    System.out.println("Found field: " + field.getName());
}

Hibernate uses Java Reflection to create the metadata of Entity class, so that the order of fields in metadata and the order of SQL columns are unpredictable.

(2) Is it possible to resolve this problem?
Yes, it can be resolved, don't use the Java Reflection API, read the bytecode(class file) directly. The order of members in bytecode is same with the declaration order in the source code. like this
First: add maven dependency to add ASM(an open source library to read/write bytecode), here we use ASM5, not ASM4
Code:
<dependency>
    <groupId>org.ow2.asm</groupId>
    <artifactId>asm-all<artifactId>
    <version>5.0.4</version>
</dependency>

Then: iterators all the fields:
Code:
// Iterates members with concurrent order, by reading bytecode file directly.
ClassReader reader = ClassReader(YourType.class.getName(), ClassReader.SKIP_CODE);
reader.accpet(new ClassVisitor(Opcodes.ASM_5, null) {
      @Override
      public void visitField(int acc, String name, String desc, String signature) {
            System.out.println("Found field: " + name);
      }
});

your problem can be resolved if Hibernate uses this way to build metadata for Entity class in the future.


Top
 Profile  
 
 Post subject: Re: SchemaExport export DDL fields ordered by field name
PostPosted: Tue Dec 01, 2015 7:58 am 
Newbie

Joined: Sun Nov 23, 2003 4:34 am
Posts: 6
Hellow babyfish!!
Thank you for your tip..


Top
 Profile  
 
 Post subject: Re: SchemaExport export DDL fields ordered by field name
PostPosted: Wed Dec 02, 2015 7:12 am 
Newbie

Joined: Sun Nov 23, 2003 4:34 am
Posts: 6
I am trying to find the way how to apply the babyfish's proposal to hibernate.
I reviewed hibernate source.

SchemaExport call Configuration.generateSchemaCreationScript(Dialect dialect)
to generate DDL from entity classes.
Configuration has already table mappings in member field.
Configuration seems create table mappings from xml mapping.

Am I right?

I added annotated entity classes.

Where is point that Ihave to change code of hibernate to create DDL in declaration order in the source code
using hibernate?

I am using hibernate version 4.3.

Is there already a tools doing this way?

Thanks..


Top
 Profile  
 
 Post subject: Re: SchemaExport export DDL fields ordered by field name
PostPosted: Wed Dec 02, 2015 12:03 pm 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
Hi jaedong

Good news!, I've tested the JDK7, reflection of Java7 or Java8 can list the members by the same order with the source code.(I remember Java6's reflection list members with unknown order)

So, try to do nothing except upgrading to Java7 or Java8.

If the metadata of hibernate uses java.util.HashMap, not java.util.LinkedHashMap, it's still not useful.


Top
 Profile  
 
 Post subject: Re: SchemaExport export DDL fields ordered by field name
PostPosted: Thu Dec 03, 2015 6:26 am 
Newbie

Joined: Sun Nov 23, 2003 4:34 am
Posts: 6
Thank you babyfish. very very much.
I remenber hibernate use java.util.LinkedHashMap.
and I use java7 and java8.

I will retry.

Thank you again.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.