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.  [ 3 posts ] 
Author Message
 Post subject: Howto test that all fields of a table are mapped?
PostPosted: Wed Mar 18, 2009 4:19 am 
Newbie

Joined: Fri Feb 06, 2009 2:16 am
Posts: 8
Hi,

context:
- an application that uses one entity per table
- non java developers that may change the table columns

objective:
make sure that if anyone add or drop columns, an alert is raised

So, can anyone tell me if there is a way to do an introspection on a Entity bean to make sure it maps all fields of a table?

Maybe using some native SQL to get all columns of the table and then test that those are all mapped by the Entity...

but how do I get the list of mapped columns from an entity?

Thanks for any lead...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 18, 2009 4:46 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Assuming that you have access to the org.hibernate.cfg.Configuration object that was used to create the SessionFactory, you can call Configuration.getTableMappings(). It returns an iterator that gives you org.hibernate.mapping.Table objects. On each of the Table objects, you can call Table.getColumnIterator() which returns an iterator that gives you org.hibernate.mapping.Column object with information about the mapped columns in a table. This will get you information about what has been mapped. Use JDBC and the java.sql.DatabaseMetaData to get infoormation about the actual database.


Top
 Profile  
 
 Post subject: working!
PostPosted: Wed Mar 18, 2009 5:30 am 
Newbie

Joined: Fri Feb 06, 2009 2:16 am
Posts: 8
Thanks a lot for this quick answer!
I will give it a try.

I am going to post what I have done that also give me the list of mapped fields:

Code:
@Test
    public void testUsedAnnotations() {
        outLn("testUsedAnnotations");
        outLn("");
        Annotation[] annotations = e.getClass().getAnnotations();
        List<String> mappedColumns = new LinkedList();
        String columnName;

        Field[] fields = e.getClass().getDeclaredFields();
        if (fields.length > 0) {
            int fieldsLentgh = fields.length;
            Annotation[] fieldAnnotations;
            Column column;

            for (int i = 0; i < fieldsLentgh; i++) {
                outLn("Declared field:");
                outLn(" " + fields[i].toString());
                outLn(" annotations:" + fields[i].getAnnotations());
                fieldAnnotations = fields[i].getDeclaredAnnotations();
                int fieldAnnotationLength = fields[i].getDeclaredAnnotations().length;
                if (fieldAnnotationLength > 0) {
                    for (int j = 0; j < fieldAnnotationLength; j++) {
                        outLn("  Filed Declared annotation:");
                        outLn("  Type:" + fieldAnnotations[j].annotationType());
                        if (fieldAnnotations[j].annotationType().getName().equals("javax.persistence.Column")){
                             column = (Column) fieldAnnotations[j];
                             columnName = column.name();
                             outLn("   column name: " + columnName);
                             mappedColumns.add(columnName.toUpperCase());
                        }
                        outLn("");
                    }
                }
                outLn("");
            }
            outLn("");
            for (String sz : mappedColumns){
                outLn(" " + sz);
            }
        } else {
            outLn("--> There is no declared fields");
        }
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.