属性映射的列数错误:

时间:2015-09-04 06:08:03

标签: mysql hibernate spring-4

我正在使用带有mysql的hibernate 4.3.7。我无法在mysql中坚持joda tiem。当我使用这个注释     @Type(type =" org.jadira.usertype.dateandtime.joda.PersistentDateTimeWithZone"),它抛出此异常

  

属性映射列数错误:car.modifiedDate类型:org.jadira.usertype.dateandtime.joda.PersistentDateTimeWithZone

实体类:

  @Entity
 public class Car implements Serializable {
private static final long serialVersionUID = 1L;

/**
 */

@Column(name = "CAR_ID", nullable = false,length = 50)
@Basic(fetch = FetchType.EAGER)
@Id
@XmlElement
String carId;
/**
 */

@Column(name = "CAR_NAME", length = 50)
@Basic(fetch = FetchType.EAGER)
@XmlElement
String carName;

/**
 */

@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTimeWithZone")
@Column(name = "DATE_CREATED")
@Basic(fetch = FetchType.EAGER)
@XmlElement
DateTime dateCreated;


@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTimeWithZone")
@Column(name = "DATE_MODIFIED")
@Basic(fetch = FetchType.EAGER)
@XmlElement
DateTime modifiedDate;
/**
 */


@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name="MCR_CAR_ID", nullable=false, insertable=false, updatable=false)
java.util.Set<MainCar> mainCar;

//setters and getters
 }

的pom.xml

<dependency>
        <groupId>org.jadira.usertype</groupId>
        <artifactId>usertype.core</artifactId>
        <version>3.0.0.CR1</version>
</dependency>

1 个答案:

答案 0 :(得分:1)

问题可能是dateCreated,数据库中的modifiedDate列的类型为DateTime,但您的代码将保存为带有区域的DateTime。您可以尝试将dateCreatedmodifiedDate类型编辑为org.jadira.usertype.dateandtime.joda.PersistentDateTime,如下所示:

@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "DATE_CREATED")
@Basic(fetch = FetchType.EAGER)
@XmlElement
DateTime dateCreated;


@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@Column(name = "DATE_MODIFIED")
@Basic(fetch = FetchType.EAGER)
@XmlElement
DateTime modifiedDate;

如果您想使用区域保存DateTime,则必须将@Type修改为:

  @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime",
            parameters = { @Parameter(name = "databaseZone", value = "UTC"),
                 @Parameter(name = "javaZone", value = "jvm")})
        @Column(name = "DATE_MODIFIED")
        @Basic(fetch = FetchType.EAGER)
        @XmlElement
        DateTime modifiedDate;