-->
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.  [ 5 posts ] 
Author Message
 Post subject: NativeSQL Firebird System Table Result POJO
PostPosted: Sun Nov 29, 2015 5:21 pm 
Newbie

Joined: Sun Nov 29, 2015 4:27 pm
Posts: 8
Hello,

I'm new to hibernate. I use it with Firebird. He needs to process SQL query eclipses which uses the system tables Fierbird to obtain metadata. How should a POJO to get a result this query?

Code:
SELECT
IIF(b.rdb$field_name = f.rdb$field_name,1,0) AS primary_key, b.rdb$field_name AS field,
IIF(POSITION('RDB$' IN c.rdb$field_name) = 0,c.rdb$field_name,
    CASE
        WHEN (((c.rdb$field_type=14) OR (c.rdb$field_type=15)) AND (c.rdb$field_length = 16) AND (c.rdb$character_set_id =1) ) THEN 'GUID'
        WHEN (c.rdb$field_type= 261) AND (c.rdb$field_sub_type =1) THEN 'TEXT'
        WHEN (c.rdb$field_type= 261) AND (c.rdb$field_sub_type <>1) THEN 'BINARY'
        WHEN (((c.rdb$field_type=14)OR(c.rdb$field_type=15)) AND c.rdb$field_length <>16 AND c.rdb$character_set_id <>1 ) THEN 'CHAR'
        WHEN (c.rdb$field_type=40 OR c.rdb$field_type=41) THEN 'TEXT'
        WHEN (c.rdb$field_type=11 OR c.rdb$field_type=27) THEN 'DOUBLE'
        WHEN c.rdb$field_type=10 THEN 'FLOAT'
        WHEN ((c.rdb$field_type=9) OR (c.rdb$field_type=16) OR (c.rdb$field_type=45)) AND (c.rdb$field_scale < 0) THEN 'DECIMAL'
        WHEN ((c.rdb$field_type=9) OR (c.rdb$field_type=16) OR (c.rdb$field_type=45)) AND (c.rdb$field_scale >= 0) THEN 'BIGINT'
        WHEN (c.rdb$field_type=8) AND (c.rdb$field_scale < 0) THEN 'DECIMAL'
        WHEN (c.rdb$field_type=8) AND (c.rdb$field_scale >= 0) THEN 'INTEGER'
        WHEN (c.rdb$field_type=7) AND (c.rdb$field_scale < 0) THEN 'DECIMAL'
        WHEN (c.rdb$field_type=7) AND (c.rdb$field_scale >= 0) THEN 'SMALLINT'
        WHEN c.rdb$field_type=12 THEN 'DATE'
        WHEN c.rdb$field_type=13 THEN 'TIME'
        WHEN c.rdb$field_type=35 THEN 'TIMESTAMP'
        WHEN (c.rdb$field_type=37) OR (c.rdb$field_type=38) THEN 'VARCHAR'
    ELSE 'UNKNOWN' END) AS TYPE,
    COALESCE(b.rdb$null_flag,0) AS notnull,
    c.rdb$field_length AS LENGTH,
    abs(c.rdb$field_scale) AS scale,
    a.rdb$relation_name AS table_name
    FROM rdb$relations a
        INNER JOIN rdb$relation_fields b ON a.rdb$relation_name = b.rdb$relation_name
        INNER JOIN rdb$fields c ON b.rdb$field_source = c.rdb$field_name
        INNER JOIN rdb$types d ON c.rdb$field_type = d.rdb$type
        INNER JOIN rdb$relation_constraints e ON e.rdb$relation_name = a.rdb$relation_name
        INNER JOIN rdb$index_segments f ON f.rdb$index_name = e.rdb$index_name
    WHERE a.rdb$system_flag = 0 AND d.rdb$field_name = 'RDB$FIELD_TYPE' AND e.rdb$constraint_type = 'PRIMARY KEY' AND a.rdb$relation_name = ?
ORDER BY a.rdb$relation_name, b.rdb$field_id


Regards


Top
 Profile  
 
 Post subject: Re: NativeSQL Firebird System Table Result POJO
PostPosted: Mon Nov 30, 2015 2:06 am 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
Hi baczek00
All the knowledge about NativeQuery can be learned from https://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch13.html


Top
 Profile  
 
 Post subject: Re: NativeSQL Firebird System Table Result POJO
PostPosted: Mon Nov 30, 2015 1:36 pm 
Newbie

Joined: Sun Nov 29, 2015 4:27 pm
Posts: 8
Thank you.

Link know. The problem is that they are not from my database tables and system tables Firebird. For tables which I own I know how to build a POJO. I do not know how to do this for the system tables.

My Pojo for the query from the first post looks like this:

Code:
@Entity
@Table(name = "MetaDataTable")
public class MetaDataTable implements Serializable {

   private static final long serialVersionUID = 2903789139471788251L;

   @Column(name = "PRIMARY_KEY")
   private byte primaryKey;
   @Column(name = "FIELD")
   private String field;
   @Column(name = "NAME")
   private String name;
   @Column(name = "NOTNULL")
   private byte notNull;
   @Column(name = "LENGTH")
   private int length;
   @Column(name = "SCALE")
   private double scale;
   @Column(name = "TABLE_NAME")
   private String tableName;

   public byte getPrimaryKey() {
      return primaryKey;
   }

   public void setPrimaryKey(byte primaryKey) {
      this.primaryKey = primaryKey;
   }

   public String getField() {
      return field;
   }

   public void setField(String field) {
      this.field = field;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public byte getNotNull() {
      return notNull;
   }

   public void setNotNull(byte notNull) {
      this.notNull = notNull;
   }

   public int getLength() {
      return length;
   }

   public void setLength(int length) {
      this.length = length;
   }

   public double getScale() {
      return scale;
   }

   public void setScale(double scale) {
      this.scale = scale;
   }

   public String getTableName() {
      return tableName;
   }

   public void setTableName(String tableName) {
      this.tableName = tableName;
   }


but fails with the error:
Code:
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: pl.cba.lukaszbaczek.NaviSoftDatabase.client.mapping.MetaDataTable


How should correct POJO?

Regards


Top
 Profile  
 
 Post subject: Re: NativeSQL Firebird System Table Result POJO
PostPosted: Mon Nov 30, 2015 1:41 pm 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
The exception "No identifier specified for entity" means you didn't support an Id field in your entity class

For the class with "@javax.persistence.Entity", a field with "@javax.persistence.Id" or "@javax.persistence.EmbeddedId" must be declared by itself or its super class.

Do it like this

Code:
@Entity
@Table(....)
public class YourPojo {

    @javax.persistence.Id
    @Column(name = "<your primary key name>")
    private Long id;

    Other non-id fields

    getters and setters
}


Top
 Profile  
 
 Post subject: Re: NativeSQL Firebird System Table Result POJO
PostPosted: Mon Nov 30, 2015 3:13 pm 
Newbie

Joined: Sun Nov 29, 2015 4:27 pm
Posts: 8
Thanks fork help. Works


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