0

I guess hibernate is trying to assign a String value fetched from the database to long.Have done many-to-one unidirectional mapping.I'm trying to display the values from the region table in a drop down in CorporateGroupForm.jsp

CorporateGroup.java

    @Entity
    @Table(name="corporate_group")
    public class CorporateGroup extends BaseObject implements Serializable {
     private Region region;
     private Long id;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="id")
    public Region getRegion() {
    return region;
   }
    public void setRegion(Region region) {
    this.region = region;
    }

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

corporateGroupForm.jsp

    <li>
    <appfuse:label styleClass="desc" key="corporateGroupDetail.region"/>
    <select name="regionDesc">
        <option value=""><fmt:message key="select.pleaseSelect"/></option>
    <c:forEach var="region" items="${regionsList}">
            <c:set var="selected" value="${corporateGroup.region ne null and corporateGroup.region.regionDesc eq region.regionDesc}"/>
         <option ${selected ? 'selected' : ''} value="${region.regionDesc }">${region.regionDesc } </option> 
        </c:forEach>
    </select>
</li>

DB:

    CREATE TABLE `corporate_group` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`comment` text,`name` varchar(255) NOT NULL,`parent_id`bigint(20) DEFAULT NULL,`primary_contact_id` bigint(20) DEFAULT NULL,`account_manager_email` varchar(255) DEFAULT NULL,`dateCreated` datetime DEFAULT CURRENT_TIMESTAMP,`region_description` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`),UNIQUE KEY `name` (`name`),KEY `FK61BCC225C8E0340A` (`parent_id`),KEY `FK61BC225F0655E4F` (`primary_contact_id`),KEY `FK_REGION_idx` (`region_description`),CONSTRAINT `fk_region` FOREIGN KEY (`region_description`) REFERENCES `region` (`region_description`) ON DELETE NO ACTION ON UPDATE NO ACTION,CONSTRAINT `FK61BC225F0655E4F` FOREIGN KEY (`primary_contact_id`) REFERENCES `app_user` (`id`),CONSTRAINT `FK61BCC225C8E0340A` FOREIGN KEY (`parent_id`) REFERENCES `corporate_group` (`id`)) ENGINE=InnoDB AUTO_INCREMENT=843 DEFAULT CHARSET=latin1;

    CREATE TABLE `region` (`id` bigint(20) NOT NULL,`country_code` varchar(50) NOT NULL,country_name` varchar(100) NOT NULL,`time_zone` varchar(100) NOT NULL,`region_description` varchar(255) NOT NULL,PRIMARY KEY (`id`),UNIQUE KEY `description_UNIQUE` (`region_description`),KEY `id` (`id`),KEY `region_description` (`region_description`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Exception Stack Trace :

Hibernate: select corporateg0_.id as id2_, corporateg0_.account_manager_email as account2_2_, corporateg0_.comment as comment2_, corporateg0_.name as name2_, corporateg0_.parent_id as parent6_2_, corporateg0_.primary_contact_id as primary5_2_, corporateg0_.region_description as region7_2_ from corporate_group corporateg0_ order by corporateg0_.name WARN [http-bio-9080-exec-1] JDBCExceptionReporter.logExceptions(77) | SQL Error: 0, SQLState: S1009 WARN [http-bio-9080-exec-1] JDBCExceptionReporter.logExceptions(77) | SQL Error: 0, SQLState: S1009 ERROR [http-bio-9080-exec-1] JDBCExceptionReporter.logExceptions(78) | Invalid value for getLong() - 'UK -UTC +0:00' ERROR [http-bio-9080-exec-1] JDBCExceptionReporter.logExceptions(78) | Invalid value for getLong() - 'UK -UTC +0:00'

The error on the web page : Data Access Failure Hibernate operation: could not execute query; uncategorized SQLException for SQL [select corporateg0_.id as id2_, corporateg0_.account_manager_email as account2_2_, corporateg0_.comment as comment2_, corporateg0_.name as name2_, corporateg0_.parent_id as parent7_2_, corporateg0_.primary_contact_id as primary5_2_, corporateg0_.region_description as region6_2_ from corporate_group corporateg0_ order by corporateg0_.name]; SQL state [S1009]; error code [0]; Invalid value for getLong() - 'UK -UTC +0:00'; nested exception is java.sql.SQLException: Invalid value for getLong() - 'UK -UTC +0:00'

Region.java

    @Entity
    @Table(name = "region")
    public class Region extends BaseObject implements Serializable {
private static final long serialVersionUID = 1L;

private Long id;
@Id 
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

private String countryCode;
private String countryName;
private String timeZone;
private String regionDesc;

@Column(name="country_code",nullable=false)
public String getCountryCode() {
    return countryCode;
}

public void setCountryCode(String countryCode) {
    this.countryCode = countryCode;
}

@Column(name="country_name",nullable=false)
public String getCountryName() {
    return countryName;
}

public void setCountryName(String countryName) {
    this.countryName = countryName;
}
@Column(name="time_zone",nullable=false)
public String getTimeZone() {
    return timeZone;
}

public void setTimeZone(String timeZone) {
    this.timeZone = timeZone;
}

@Column(name="region_description",nullable=false)
public String getRegionDesc() {
    return regionDesc;
}

public void setRegionDesc(String regionDesc) {
    this.regionDesc = regionDesc;
}
@Override
public String toString() {
    StringBuffer strBuff = new StringBuffer();
    if (getId() != null) {
        strBuff = strBuff.append("ID:" + getId() + ",");
        strBuff = strBuff.append("Country Name:" + getCountryName() + ",");
        strBuff = strBuff.append("Country Code:" + getCountryCode() + ",");
        strBuff = strBuff.append("Timezone:" + getTimeZone() + ",");
        strBuff = strBuff.append("Region Description:" + getRegionDesc() + ",");
    }

    return strBuff.toString();
}

@Override
public boolean equals(Object o) {
    // TODO Auto-generated method stub
    if (!(o instanceof Region)) {
        return false;
    }
    Region reg = (Region) o;

    return !(regionDesc != null ? !regionDesc.equals(reg.getRegionDesc()) : reg.getRegionDesc() != null);
}

@Override
public int hashCode() {
    // TODO Auto-generated method stub
    int hashcode = 0;
    if (this.regionDesc != null) {
        hashcode = hashcode + this.regionDesc.hashCode();
    }

    return hashcode;
}

}

Now a different error :

    ERROR [localhost-startStop-1 ContextLoader.initWebApplicationContext(215) | Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_filterChainProxyPostProcessor': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAdvisor': Cannot create inner bean '(inner bean)' of type [org.springframework.transaction.interceptor.TransactionInterceptor] while setting bean property 'transactionInterceptor'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [applicationContext-dao.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext-dao.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.canvas8.model.CorporateGroup column: id (should be mapped with insert="false" update="false")
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:405)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:220)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:881)
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:606)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:366)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1081)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1877)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

2 Answers2

0

In your CorporateGroup entity class, you mapped region with region_description of Region entity which has a primary key of Long

@ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinColumn(name="region_description") 
    public Region getRegion() { 
      return region; 
    }

What you can do is you can map region member variable of CorporateGroup to primary key of Region entity class not the region_description.

Regarding this error :

Repeated column in mapping for entity: com.canvas8.model.CorporateGroup column: id (should be mapped with insert="false" update="false")

The error message is obvious, you have mapped same column twice. Refer this and fix the issue.

Hope this helps!

harshavmb
  • 3,404
  • 3
  • 21
  • 55
0

I was able to specify the referenced column name to resolve the issue for me. I didn't want to eager fetch the entities since it would only be used sometimes and didn't have permissions to modify the database schema.

Try this:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "region_description", referencedColumnName = "region_description")
private Region region;
wheeleruniverse
  • 1,511
  • 2
  • 20
  • 37