可以将 Hibernate 的 @Id 设置为字符串吗?

huangapple go评论65阅读模式
英文:

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: &quot;officeCode&quot;
	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 = &quot;offices&quot;)
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&lt;Office&gt; selectOffices() {
		return sessionFactory.getCurrentSession().createQuery(&quot;from Office&quot;)
				.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&lt;Office&gt; 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({ &quot;/&quot;, &quot;/index&quot; })
	public ModelAndView loadIndex(ModelAndView model) {
		List&lt;Office&gt; officeList = officeServiceImpl.selectOffices();
		model.addObject(&quot;officeList&quot;, officeList);
		model.setViewName(&quot;index&quot;);
		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=&quot;uuid&quot;)
@GenericGenerator(name=&quot;uuid&quot;, strategy=&quot;uuid2&quot;)
private String officeCode;

</details>



huangapple
  • 本文由 发表于 2020年7月24日 02:27:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63060840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定