你好:)我真的希望我能在这里找到帮助!
问题:我使用Spring JPA,创建了基本的REST服务,添加了数据库,并且可以通过entitymanager.createQuery()在databaste中找到数据
但是当我尝试搜索时间戳或日期时间时,它给了我这个错误:
15:12 SELECT b FROM StationsMessung b WHERE b.AVNR=:AVNR AND
b.TXNR=:TXNR AND b.DBTM=:DATUM
java.lang.ClassCastException: java.lang.String cannot
be cast to java.util.Date
at org.hibernate.type.descriptor.java.JdbcTimestampTypeDescriptor.unwrap(JdbcTimestampTypeDescriptor.java:24)
at org.hibernate.type.descriptor.sql.TimestampTypeDescriptor$1.doBind(TimestampTypeDescriptor.java:48)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:74)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:53)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:628)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2001)
at
org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1915)
at org.hibernate.... (show balloon)
我知道它说不能将String转换为java.util.Date,但是我不知道如何解决它。感谢每个帮助者!
///////////////////////////////////////////////// ////////// ENTITY CLASS
@Entity
@IdClass(StationsMessung.class)
@Table(name = "****", schema = "***")
public class StationsMessung implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "AVNR")
private int AVNR; ////
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "TXNR")
private int TXNR;
@GeneratedValue(strategy = GenerationType.AUTO)
@Temporal(TemporalType.TIMESTAMP)
@Type(type = "date")
@Column(name = "DBTM")
private Timestamp DBTM;
public StationsMessung(int AVNR, int TXNR, Timestamp DBTM) {
this.AVNR = AVNR;
this.TXNR = TXNR;
this.DBTM = DBTM;
}
public StationsMessung() {
}
public int getAvnr() {
return AVNR;
}
public void setAvnr(int AVNR) {
this.AVNR = AVNR;
}
public int getTxnr() {
return TXNR;
}
public void setTxnr(int TXNR) {
this.TXNR = TXNR;
}
public Timestamp getDBTM() {
return DBTM;
}
public void setDBTM(Timestamp DBTM) {
this.DBTM = DBTM;
}
///////////////////////////////////////////////// //////////// SERVICE CLASS
@Service
public class StationService {
@PersistenceContext
EntityManager entityManager;
public List<Station> getAllStationMessungen(int AVNR, int TXNR, Timestamp DATUM) {
return entityManager.createQuery("SELECT b FROM StationsMessung b WHERE b.AVNR=:AVNR AND b.TXNR=:TXNR AND b.DBTM=:DATUM")
.setParameter("TXNR",TXNR )
.setParameter("AVNR",AVNR )
.setParameter("DATUM", DATUM)
.getResultList();
}
///////////////////////////////////////////////// ///////////// CONTROLLER CLASS
@RestController
@RequestMapping("/station")
public class StationController {
@Autowired //This annotation allows Spring to resolve and inject
collaborating beans into your bean
StationService stationService; //service
@RequestMapping(value = "/allmessungen/{AVNR}/{TXNR}/{DATUM}", method =
RequestMethod.GET)
public List<Station> getAllStationMessungen(@PathVariable int AVNR, int
TXNR, Timestamp DATUM) {
return stationService.getAllStationMessungen( AVNR, TXNR, DATUM);
}
现在,当我搜索没有时间戳的数据时,它会向我显示数据,并且可以正常工作。
当我使用时间戳或日期时,它向我显示上面的错误。
我的输入是:
PARAM.1 AVNR:716
PARAM.2 TXNR:1339
PARAM.3 DBTM:2014-01-04 05:30:00
(其格式为yyyy-mm-dd-hh24:mi:ss)
感谢您的帮助:)