-->
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: SQLGrammarException for Unmapped Properties
PostPosted: Wed Dec 02, 2009 10:38 am 
Newbie

Joined: Mon Apr 16, 2007 2:30 am
Posts: 8
Location: Lahore
hi,
i am facing SQLGrammarException while getting Entity from DB, actually i have few properties witch are not mapped with column names, and at loading object the system throws the Exception.

WARN [org.hibernate.util.JDBCExceptionReporter] - <SQL Error: 1054, SQLState: 42S22>
ERROR [org.hibernate.util.JDBCExceptionReporter] - <Unknown column this_.cityAsString' in 'field list'>
org.hibernate.exception.SQLGrammarException: could not execute query at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)

My class code is like,

@Entity
@Table(name = "RETAILER")
public class RetailerEntity extends MobexEntity implements IRetailerEntity {

private static final long serialVersionUID = 1L;
private Long id;
private Long bankId;
private String cityAsString = null;

@Column(name = "id", nullable = false)
@Id()
@OrderBy()
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}

@Column(name = "BANK_ID")
public Long getBankId() {
return bankId;
}
public void setBankId(Long bankId) {
this.bankId = bankId;
}

// UnMapped
public String getCityAsString() {
return cityAsString;
}

public void setCityAsString(String cityAsString) {
this.cityAsString = cityAsString;
}
}

due scenario i can't make cityAsString as Transient.

Kindly give me you feedback

Kashif Bashir

kashefbasher@gmail.com

_________________
Kashif Bashir

Mobex Limited
kashefbasher@gmail.com


Top
 Profile  
 
 Post subject: Re: SQLGrammarException for Unmapped Properties
PostPosted: Wed Dec 02, 2009 11:17 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
But isn't a "private transient <field>" something different as a @Transient annotated field?

What scenario can't you use transient for?

If you have to manually serialize the class and have to serialize the cityAsString field with it, you cannot mark is as "private transient cityAsString = null;" I supose.
But if you mark it @Transient, are there still any problems?


Top
 Profile  
 
 Post subject: Re: SQLGrammarException for Unmapped Properties
PostPosted: Thu Dec 03, 2009 1:33 am 
Newbie

Joined: Mon Apr 16, 2007 2:30 am
Posts: 8
Location: Lahore
stevo,

i am using this object as VO and passing to view layer which is on another server, while using transient, the cityAsString became nulll and lost value, so i have to omit the transient modifier but not using persistence annotation (not mapping with DB), but i am astonished to see that why hibernate is fetching the field which is not mapped with any table.column.

_________________
Kashif Bashir

Mobex Limited
kashefbasher@gmail.com


Top
 Profile  
 
 Post subject: Re: SQLGrammarException for Unmapped Properties
PostPosted: Thu Dec 03, 2009 3:56 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
By default, the @Basic annotation is used on every field (private fields or getters that is).
If you ommit the annotation and if you're using JPA / EntityManager (I supose, only used them in a very small education project at home) I read in the Hibernate Annotation docs that it will be implied as an @Basic field and the EntityManager will treat the field as @Basic persistent, unless you mark it as @Transient.

But ofcourse, if you set the cityAsString through code somewhere (it is initialized as null in your VO class) but afterwards fetch the VO from the persistency layer, a new instance will be created and cityAsString will be initially null again.

Transient fields should be "calculated" or "formatted" fields based on persistent information from within the class.
So for example, you might have the following (unless the city field is in your MobexEntity ... don't know the contents of thatone):
Code:
@Entity
@Table(name = "RETAILER")
public class RetailerEntity extends MobexEntity implements IRetailerEntity {

    private static final long serialVersionUID = 1L;
    private Long id;
    private Long bankId;
    private City city = null;

    @Column(name = "id", nullable = false)
    @Id()
    @OrderBy()
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "BANK_ID")
    public Long getBankId() {
        return bankId;
    }
    public void setBankId(Long bankId) {
        this.bankId = bankId;
    }

    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
    @JoinColumn(name="CITY_ID")
    public City getCity () {
        return this.city;
    }
    public void setCity(City city) {
        this.city = city;
    }

    @Transient
    public String getCityAsString() {
        return city.getName();
    }

}


Otherwise, cityAsString will be null every time you load/find it from the EntityManager I guess.

See the following URL for the "mapped even if no Annotation is present" - feature:
http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#entity-mapping-property
Quote:
Every non static non transient property (field or method) of an entity bean is considered persistent, unless you annotate it as @Transient. Not having an annotation for your property is equivalent to the appropriate @Basic annotation. The @Basic annotation allows you to declare the fetching strategy for a property:

public transient int counter; //transient property

private String firstname; //persistent property

@Transient
String getLengthInMeter() { ... } //transient property

String getName() {... } // persistent property

@Basic
int getLength() { ... } // persistent property
...


Top
 Profile  
 
 Post subject: Re: SQLGrammarException for Unmapped Properties
PostPosted: Thu Dec 03, 2009 5:44 am 
Newbie

Joined: Mon Apr 16, 2007 2:30 am
Posts: 8
Location: Lahore
thanks for quick reply stevo,

i have fixed that problem after using

@Transient String cityAsString = null;

@Transient
public String getCityAsString() {
return cityAsString;
}
public void setCityAsString(String cityAsString) {
this.cityAsString = cityAsString;
}

now after getting data in entity from db i puts city name in cityAsString, and it do not lost value on view layer now before that i was using
transient String cityAsString = null;
instead of
@Transient String cityAsString = null;

_________________
Kashif Bashir

Mobex Limited
kashefbasher@gmail.com


Top
 Profile  
 
 Post subject: Re: SQLGrammarException for Unmapped Properties
PostPosted: Thu Dec 03, 2009 5:56 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
Ok kashefbasher,

I supose your problem is solved now? :-)

But, for details ... It is better to only use an Annotation (the @Transient in this case) OR on the private field OR on the getter method, depending on where you specify your @Id annotation.
If your @Id annotation is on a private field (private Long id;) Hibernate will supose all your JPA / Hibernate Annotations are on the fields and will ignore annotations on the getters (I don't know if he'll ignore Hibernate-Validator, Envers, ... annotations as well).
If your @Id annotation is on a getter, Hibernate will only check the getters for persistency annotations.

So, in your case, I think your @Tranient annotation on the getCityAsString() method is "useless" because it is ignored by Hibernate. It can be removed, so that only the private String cityAsString = null; is marked with the @Transient annotation.

EDIT:
I was slightly wrong.
Your @Id annotation is on your getter.
So the @Transient on your private field is ignored by Hibernate, and the getter getCityAsString must be annotated.


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.