1

I have an Entity class

    @Entity
    @Table(name = "rule")
    public class Rule implements Cloneable, Serializable, IPojoGenEntity, IRule, SequencedEntity {

    private String name;

    private Service service;

    //getter .. setter for service and name

      public String getServiceName() {
        return (this.service.getName());
      }

     public void setServiceName(String servicename) {
       this.service.setName(servicename);
     }

    }

I am getting exception for getting service name through RulClass object

public String getServiceName() {
        return (this.service.getName());
      }

Stack Trace

Caused by: com.ibm.db2.jcc.b.SqlException: "RULE0_.SERVICENAME" is not valid in the context where it is used.
    at com.ibm.db2.jcc.b.fg.e(fg.java:1596)
    at com.ibm.db2.jcc.b.fg.a(fg.java:1206)
    at com.ibm.db2.jcc.a.gb.g(gb.java:140)
    at com.ibm.db2.jcc.a.gb.a(gb.java:39)
    at com.ibm.db2.jcc.a.w.a(w.java:34)
    at com.ibm.db2.jcc.a.vb.g(vb.java:139)

Can we use such getter and setter in an entity class?

I am using hibernate, spring, DB2, IBM WebSphere

Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190

3 Answers3

1

You should either make it @Transient as it was mentioned if you don't want to store it

OR

Define @javax.persistence.Column(name = "service_id") field annotation for the getter to let hibernate know which column to use.

OR

Rename DB to have the service field "SERVICENAME" to use default column name

StanislavL
  • 56,971
  • 9
  • 68
  • 98
0

There is the Transient annotation to tell Hibernate to ignore a field. So:

@Transient
private Service service;

From very similar SO question: Make hibernate ignore class variables that are not mapped.

Community
  • 1
  • 1
SimonC
  • 6,590
  • 1
  • 23
  • 40
0

As serviceName was not a member of Rule class so there is a problem with method name. Name cannot be like

getServiceName
setServiceName

rather it should be something other than get or set prefix

fetchServiceName
addServiceName
Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190