英文:
Can a Hibernate @Id be a string?
问题
我有一个包含一些公司办公室信息的表,其Id是一个字符串。我尝试使用@Id
标签在Hibernate中进行映射,但是出现了java.lang.NumberFormatException
的错误。
这让我想知道是否可以使用字符串作为Id,或者我是否遗漏了什么?
以下是错误信息:
Jul 23, 2020 1:18:09 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet [jsp] threw exception
java.lang.NumberFormatException: For input string: "officeCode"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at javax.el.ListELResolver.coerce(ListELResolver.java:150)
at javax.el.ListELResolver.getValue(ListELResolver.java:67)
...
这是办公室类:
package com.ver.company.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "offices")
public class Office implements Serializable {
public Office() {}
@Id
private String officeCode;
@Column
private String city;
@Column
private String phone;
@Column
private String addressLine1;
@Column
private String addressLine2;
@Column
private String state;
@Column
private String country;
@Column
private String postalCode;
@Column
private String territory;
}
Dao实现:
package dom.ver.company.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import dom.ver.company.model.Office;
@Repository
public class OfficeDaoImpl implements OfficeDao {
public OfficeDaoImpl() {}
@Autowired
private SessionFactory sessionFactory;
public void insertOffice(Office office) {
sessionFactory.getCurrentSession().saveOrUpdate(office);
}
public List<Office> selectOffices() {
return sessionFactory.getCurrentSession().createQuery("from Office")
.list();
}
}
Service:
package dom.ver.company.service;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import dom.ver.company.OfficeDao;
import dom.ver.company.dao.OfficeDaoImpl;
import dom.ver.company.model.Office;
@Service
@Transactional
public class OfficeServiceImpl implements OfficeService {
@Autowired
private OfficeDao officeDao;
@Override
@Transactional
public List<Office> selectOffices() {
return officeDao.selectOffices();
}
}
Controller类:
package dom.ver.company.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import dom.ver.company.model.Office;
import dom.ver.company.service.OfficeService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
@Controller
public class OfficeController {
@Autowired
OfficeService officeServiceImpl;
OfficeController(){}
@RequestMapping({ "/", "/index" })
public ModelAndView loadIndex(ModelAndView model) {
List<Office> officeList = officeServiceImpl.selectOffices();
model.addObject("officeList", officeList);
model.setViewName("index");
return model;
}
}
英文:
I have a table with some information of company offices and its Id is a string. I'm trying to map it in Hibernate with the @Id
tag but its giving me an error for java.lang.NumberFormatException
This causes me to wonder if its possible to use strings as Ids or if I'm missing something?
Here is the error:
Jul 23, 2020 1:18:09 PM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet [jsp] threw exception
java.lang.NumberFormatException: For input string: "officeCode"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at javax.el.ListELResolver.coerce(ListELResolver.java:150)
at javax.el.ListELResolver.getValue(ListELResolver.java:67)
...
This is the office class:
package com.ver.company.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "offices")
public class Office implements Serializable{
public Office() {}
@Id
private String officeCode;
@Column
private String city;
@Column
private String phone;
@Column
private String addressLine1;
@Column
private String addressLine2;
@Column
private String state;
@Column
private String country;
@Column
private String postalCode;
@Column
private String territory;
}
}
Dao implementation:
package dom.ver.company.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import dom.ver.company.model.Office;
@Repository
public class OfficeDaoImpl implements OfficeDao {
public OfficeDaoImpl () {}
@Autowired
private SessionFactory sessionFactory;
public void insertOffice(Office office) {
sessionFactory.getCurrentSession().saveOrUpdate(office);
}
public List<Office> selectOffices() {
return sessionFactory.getCurrentSession().createQuery("from Office")
.list();
}
}
Service:
package dom.ver.company.service;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import dom.ver.company.OfficeDao;
import dom.ver.company.dao.OfficeDaoImpl;
import dom.ver.company.model.Office;
@Service
@Transactional
public class OfficeServiceImpl implements OfficeService {
@Autowired
private OfficeDao officeDao;
@Override
@Transactional
public List<Office> selectOffices() {
return officeDao.selectOffices();
}
}
Controller class:
package dom.ver.company.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import dom.ver.company.model.Office;
import dom.ver.company.service.OfficeService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
@Controller
public class OfficeController {
@Autowired
OfficeService officeServiceImpl;
OfficeController(){}
@RequestMapping({ "/", "/index" })
public ModelAndView loadIndex(ModelAndView model) {
List<Office> officeList = officeServiceImpl.selectOffices();
model.addObject("officeList", officeList);
model.setViewName("index");
return model;
}
}
答案1
得分: 3
如果您不指定生成策略,Hibernate将使用GenerationType.AUTO
,而这不适用于String
。
您可以像这样使用它:
@Id
@GeneratedValue(generator="uuid")
@GenericGenerator(name="uuid", strategy="uuid2")
private String officeCode;
英文:
If you don't specify the generation strategy, Hibernate will use GenerationType.AUTO
, which is not applicable to String
.
You can use it like this:
@Id
@GeneratedValue(generator="uuid")
@GenericGenerator(name="uuid", strategy="uuid2")
private String officeCode;
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论