我正在开发Java play项目并尝试为项目用户创建时间跟踪器。我有以下课程用于跟踪时间:
@Entity(name = "trackings")
public class Tracking extends Model {
@EmbeddedId
public TrackingPK id;
@ManyToOne
@MapsId("user")
@JoinColumn(name="user", referencedColumnName="id")
public User user;
@ManyToOne
@MapsId("project")
@JoinColumn(name="project", referencedColumnName="id")
public Project project;
@Required
@Enumerated(EnumType.STRING)
public ETrackingDuration duration;
@Embeddable
public class TrackingPK implements Serializable {
@ManyToOne
@JoinColumn(name = "user_id", referencedColumnName = "id", insertable = false, updatable = false)
private User user;
@ManyToOne
@JoinColumn(name = "project_id", referencedColumnName = "id", insertable = false, updatable = false)
private Project project;
@Column(name="date", columnDefinition="DATE NOT NULL")
@Temporal(TemporalType.DATE)
private Date date;
}
}
不幸的是,JPA将日期保持为auto_increment
,我不明白为什么。
以下是我的日志:
09:14:16,939 ERROR ~ HHH000388: Unsuccessful: create table trackings (date datetime not null auto_increment, project_id bigint not null, user_id bigint not null, id bigint not null, duration varchar(255), primary key (date, project_id, user_id, id)) ENGINE=InnoDB DEFAULT CHARSET=utf8
09:14:16,939 ERROR ~ Incorrect column specifier for column 'date'
09:14:16,941 ERROR ~ HHH000388: Unsuccessful: alter table trackings add index FK_fjady3y2u9ej76atpce4djsu5 (project_id), add constraint FK_fjady3y2u9ej76atpce4djsu5 foreign key (project_id) references projects (id)
09:14:16,941 ERROR ~ Table 'trackings' doesn't exist
09:14:16,944 ERROR ~ HHH000388: Unsuccessful: alter table trackings add index FK_rdi49oyr8mro9t6vjj5towo0b (user_id), add constraint FK_rdi49oyr8mro9t6vjj5towo0b foreign key (user_id) references users (id)
外键是正确的但不是date
...
答案 0 :(得分:0)
我可以看到映射存在一些问题:
@MapsId("user")
和@MapsId("project")
分别需要Tracking.user
和Tracking.project
@Column
和TrackingPK.user
上的TrackingPK.project
注释是多余的@MapsId
(如果您在JPA中定义了两次相同的列,实际上可能遇到问题,没有在其中一个定义中声明insertable=false, updatable=false
)java.util.Date
只要时间类型设置为TemporalType.DATE
(请参阅:https://docs.oracle.com/javaee/7/tutorial/persistence-intro001.htm;您的JPA提供商可能会允许它,但是你需要检查)关于模式定义问题,如果所有其他方法都失败,请尝试使用columnDefinition="date not null"
覆盖该列的默认SQL。
编辑结果play.db.jpa.Model
定义了@Id
列。混合@Id
和@EmbeddedId
是导致问题的原因。您希望从play.db.jpa.GenericModel
延伸。