Hibernate 从数据库加载joda LocalTime
我正在使用Joda Time和Jadira用户类型库在数据库中持久化我的Joda LocalTime 这是我的实体Hibernate 从数据库加载joda LocalTime,hibernate,jodatime,Hibernate,Jodatime,我正在使用Joda Time和Jadira用户类型库在数据库中持久化我的Joda LocalTime 这是我的实体 @Entity public class Airport implements Serializable { private static final long serialVersionUID = 6382228467347085567L; @Id @GeneratedValue(strategy = GenerationType.AUTO)
@Entity
public class Airport implements Serializable {
private static final long serialVersionUID = 6382228467347085567L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
@NotNull
private String nameOACI;
@Column(nullable = false)
@NotNull
private String nameIATA;
@Column(nullable = false)
@NotNull
private int ssliaLevel;
@Column(nullable = true)
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalTime")
private LocalTime openingTime;
@Column(nullable = true)
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalTime")
private LocalTime closingTime;
@Column(nullable = false)
@NotNull
private String latitude;
@Column(nullable = false)
@NotNull
private String longitude;
@Column(nullable = false)
@NotNull
private int altitude;
@Column(nullable = true)
private String comments;
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private AirportCategoryE category;
@Column(nullable = true)
@NotNull
@Type(type = "com.flightfaq.dao.converters.MinutesDaoConverter")
private Minutes taxiTime;
@Column(nullable = true)
@NotNull
@Type(type = "com.flightfaq.dao.converters.MinutesDaoConverter")
private Minutes depTimeOffset;
@Column(nullable = true)
@NotNull
@Type(type = "com.flightfaq.dao.converters.MinutesDaoConverter")
private Minutes arrTimeOffset;
@OneToMany(mappedBy = "airport", cascade = CascadeType.ALL)
private List<Runway> runways;
@OneToMany(mappedBy = "airport", cascade = CascadeType.ALL)
private List<Fueler> fuelers;
@OneToMany(mappedBy = "airport", cascade = CascadeType.ALL)
private List<Handling> handlings;
public Airport() {
runways = new ArrayList<Runway>();
fuelers = new ArrayList<Fueler>();
handlings = new ArrayList<Handling>();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNameOACI() {
return nameOACI;
}
public void setNameOACI(String nameOACI) {
this.nameOACI = nameOACI;
}
public String getNameIATA() {
return nameIATA;
}
public void setNameIATA(String nameIATA) {
this.nameIATA = nameIATA;
}
public int getSsliaLevel() {
return ssliaLevel;
}
public void setSsliaLevel(int ssliaLevel) {
this.ssliaLevel = ssliaLevel;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public int getAltitude() {
return altitude;
}
public void setAltitude(int altitude) {
this.altitude = altitude;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public AirportCategoryE getCategory() {
return category;
}
public void setCategory(AirportCategoryE category) {
this.category = category;
}
public Minutes getTaxiTime() {
return taxiTime;
}
public void setTaxiTime(Minutes taxiTime) {
this.taxiTime = taxiTime;
}
public Minutes getDepTimeOffset() {
return depTimeOffset;
}
public void setDepTimeOffset(Minutes depTimeOffset) {
this.depTimeOffset = depTimeOffset;
}
public Minutes getArrTimeOffset() {
return arrTimeOffset;
}
public void setArrTimeOffset(Minutes arrTimeOffset) {
this.arrTimeOffset = arrTimeOffset;
}
public List<Runway> getRunways() {
return runways;
}
public void setRunways(List<Runway> runways) {
this.runways = runways;
}
public List<Fueler> getFuelers() {
return fuelers;
}
public void setFuelers(List<Fueler> fuelers) {
this.fuelers = fuelers;
}
public List<Handling> getHandlings() {
return handlings;
}
public void setHandlings(List<Handling> handlings) {
this.handlings = handlings;
}
public LocalTime getOpeningTime() {
return openingTime;
}
public void setOpeningTime(LocalTime openingTime) {
this.openingTime = openingTime;
}
public LocalTime getClosingTime() {
return closingTime;
}
public void setClosingTime(LocalTime closingTime) {
this.closingTime = closingTime;
}
}
而且
Can not set org.joda.time.LocalTime field com.flightfaq.beans.business.Airport.closingTime to org.joda.time.LocalTime
下面是我用来在数据库中输入问题机场的SQL命令
INSERT INTO `Airport` (`id`, `altitude`, `arrTimeOffset`, `category`, `closingTime`, `comments`, `depTimeOffset`, `latitude`, `longitude`, `nameIATA`, `nameOACI`, `openingTime`, `ssliaLevel`, `taxiTime`) VALUES
(1, 499, '0', 'A', '00:00:00', 'se garer au parking R12', '15', '433806N', '0012204E', 'TLS', 'LFBO', '00:00:00', 4, '0');
我输入了“00:00:00”作为关闭时间。您的
closingTime
应该在数据库中输入time
。如果您想在数据库中使用
varchar[]
类型,那么您应该使用自己的用户类型
应该是这样的
public class LocalTimeType implements UserType {
public int[] sqlTypes() {
return new int[] {
Types.VARCHAR,
};
}
public Class getReturnedClass() {
return LocalTime.class;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws SQLException {
...
}
public void nullSafeSet(PreparedStatement st, Object value, int index) throws SQLException {
...
}
...
}
我正在考虑实现我自己的UserType,将Joda'LocalTime'转换为字符串。(例如LocalTime:08h23m=>String“0823”)您所说的是,如果我手动将LocalTime转换为varchar数据库对象。这可能就是我要做的。但是使用jadira,数据库中的类型被正确设置为TIME。我不明白这个例外
Can not set org.joda.time.LocalTime field com.flightfaq.beans.business.Airport.closingTime to org.joda.time.LocalTime
INSERT INTO `Airport` (`id`, `altitude`, `arrTimeOffset`, `category`, `closingTime`, `comments`, `depTimeOffset`, `latitude`, `longitude`, `nameIATA`, `nameOACI`, `openingTime`, `ssliaLevel`, `taxiTime`) VALUES
(1, 499, '0', 'A', '00:00:00', 'se garer au parking R12', '15', '433806N', '0012204E', 'TLS', 'LFBO', '00:00:00', 4, '0');
public class LocalTimeType implements UserType {
public int[] sqlTypes() {
return new int[] {
Types.VARCHAR,
};
}
public Class getReturnedClass() {
return LocalTime.class;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws SQLException {
...
}
public void nullSafeSet(PreparedStatement st, Object value, int index) throws SQLException {
...
}
...
}