我正在关注Hibernate: Use a Base Class to Map Common Fields和openjpa inheritance tutorial,以便在基表中放置ID,lastModifiedDate等常用列。
我的注释映射如下:
BaseTable:
@MappedSuperclass
public abstract class BaseTable {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "lastmodifieddate")
private Date lastModifiedDate;
...
人员表 -
@Entity
@Table(name = "Person ")
public class Person extends BaseTable implements Serializable{
@Column(name = "name")
private String name;
...
创建生成的语句:
create table Person (id integer not null auto_increment, lastmodifieddate datetime, name varchar(255), primary key (id)) ;
将Person对象保存到db后,
Person p = new Person();
p.setName("Test");
p.setLastModifiedDate(new Date());
..
getSession().save(p);
我正在设置日期字段但是,它正在保存带有生成ID和LastModifiedDate = null的记录,而Name =" Test"。
插入声明:
insert into Person (lastmodifieddate, name) values (?, ?)
binding parameter [1] as [TIMESTAMP] - <null>
binding parameter [2] as [VARCHAR] - Test
通过ID查询读取: 当我按照下面的方式执行休眠查询(get By ID)时,它按给定的ID读取人。
Criteria c = getSession().createCriteria(Person.class);
c.add(Restrictions.eq("id", id));
Person person= c.list().get(0);
//person has generated ID, LastModifiedDate is null
select query select person0_.id as id8_, person0_.lastmodifieddate as lastmodi8_, person0_.name as name8_ from Person person0_
- Found [1] as column [id8_]
- Found [null] as column [lastmodi8_]
- Found [Test] as column [name8_ ]
ReadAll query:
Query query = getSession().createQuery("from " + Person.class.getName());
List<Person> allPersons=query.list();
读取所有的对应SQL
select query select person0_.id as id8_, person0_.lastmodifieddate as lastmodi8_, person0_.name as name8_ from Person person0_
- Found [1] as column [id8_]
- Found [null] as column [lastmodi8_]
- Found [Test] as column [name8_ ]
- Found [2] as column [id8_]
- Found [null] as column [lastmodi8_]
- Found [Test2] as column [name8_ ]
但是当我在控制台中打印出列表时,它更加奇怪。它正在选择人物对象列表
我不知道这里有什么不对。你能看一下吗?
FYI,
My Hibernate-core version:4.1.2,MySQL Connector version:5.1.9。
总之,这里有两个问题
答案 0 :(得分:1)
使用
@Temporal(TemporalType.DATE)
<{1}} 上的注释
用于ID生成使用策略。
Date
或
@GeneratedValue(strategy=GenerationType.AUTO)
或使用sequance
@GeneratedValue(strategy=GenerationType.IDENTITY)
您也可以在您的子类中进行属性访问:
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "custom_generator")
@SequenceGenerator(name = "custom_generator", sequenceName = "sequance_table")
和
public abstract class BaseTable {
protected int id;
protected Date lastModifiedDate;
// ...
}
答案 1 :(得分:0)
在BaseTable类中尝试这个
@Temporal(value = TemporalType.TIMESTAMP)
@Column(name = "lastmodifieddate", nullable = false,
columnDefinition = "Timestamp default CURRENT_TIMESTAMP")
@org.hibernate.annotations.Generated(value = GenerationTime.ALWAYS)
public Date getLastModifiedDate( )
{
return this.lastModifiedDate;
}
其中BaseTable类应该有@MappedSuperclass。并且实体中必须有一个生成的日期字段。
@MappedSuperclass
public abstract class BaseTable {
protected int id;
protected Date lastModifiedDate;
@Id
@GeneratedValue(strategy = GenerationType.AUTO) // For Mysql
// For Oracle or MSSQL @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public int getId()
{
return this.id;
}
@Temporal(value = TemporalType.TIMESTAMP)
@Column(name = "lastmodifieddate", nullable = false,
columnDefinition = "Timestamp default CURRENT_TIMESTAMP")
@org.hibernate.annotations.Generated(value = GenerationTime.ALWAYS)
public Date getLastModifiedDate( )
{
return this.lastModifiedDate;
}
}