java 休眠注释-使用@MappedSuperclass 继承-值未设置为基类字段-奇怪的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11064986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hibernate annotation- inheritance with @MappedSuperclass - values are not being set to base class fields- strange error
提问by gtiwari333
I was following Hibernate: Use a Base Class to Map Common Fieldsand openjpa inheritance tutorialto put common columns like ID, lastModifiedDate etc in base table.
我正在关注 Hibernate:使用基类映射公共字段和 openjpa 继承教程将公共列(如 ID、lastModifiedDate 等)放在基表中。
My annotated mappings are as follow :
我的注释映射如下:
BaseTable :
基表:
@MappedSuperclass
public abstract class BaseTable {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "lastmodifieddate")
private Date lastModifiedDate;
...
Person table -
人表——
@Entity
@Table(name = "Person ")
public class Person extends BaseTable implements Serializable{
@Column(name = "name")
private String name;
...
Create statement generated :
创建语句生成:
create table Person (id integer not null auto_increment, lastmodifieddate datetime, name varchar(255), primary key (id)) ;
After I save a Person object to db,
在我将 Person 对象保存到数据库后,
Person p = new Person();
p.setName("Test");
p.setLastModifiedDate(new Date());
..
getSession().save(p);
I am setting the date field but, it is saving the record with generated ID and LastModifiedDate = null, and Name="Test".
我正在设置日期字段,但它使用生成的 ID 和 LastModifiedDate = null 以及 Name="Test" 保存记录。
Insert Statement :
插入语句:
insert into Person (lastmodifieddate, name) values (?, ?)
binding parameter [1] as [TIMESTAMP] - <null>
binding parameter [2] as [VARCHAR] - Test
Read by ID query :When I do hibernate query (get By ID) as below, It reads person by given ID.
按 ID 查询读取:当我按如下方式进行休眠查询(按 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 :
ReadAll 查询:
Query query = getSession().createQuery("from " + Person.class.getName());
List<Person> allPersons=query.list();
Corresponding SQL for read all
读取全部对应的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_ ]
But when I print out the list in console, its being more weird. it is selecting List of Person object with
但是当我在控制台中打印出列表时,它变得更奇怪了。它正在选择人员对象列表
- ID fields = all 0 (why all 0 ?)
- LastModifiedDate = null
- Name fields have valid values
- ID 字段 = 全 0(为什么全是 0?)
- LastModifiedDate = 空
- 名称字段具有有效值
I don't know whats wrong here. Could you please look at it?
我不知道这里出了什么问题。你能看一下吗?
FYI,
供参考,
My Hibernate-core version : 4.1.2, MySQL Connector version : 5.1.9 .
我的 Hibernate 核心版本:4.1.2,MySQL 连接器版本:5.1.9。
In summary, There are two issues here
总结一下,这里有两个问题
- Why I am getting All ID Fields =0 when using read all?
- Why the LastModifiedDate is not being inserted?
- 为什么我在使用 read all 时得到 All ID Fields =0?
- 为什么未插入 LastModifiedDate?
回答by Ilya
Use
利用
@Temporal(TemporalType.DATE)
annotation on Date
注释在 Date
For ID generation use strategy.
对于 ID 生成使用策略。
@GeneratedValue(strategy=GenerationType.AUTO)
or
或者
@GeneratedValue(strategy=GenerationType.IDENTITY)
or using sequance
或使用序列
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "custom_generator")
@SequenceGenerator(name = "custom_generator", sequenceName = "sequance_table")
also you can make property access in your child class:
您也可以在子类中访问属性:
public abstract class BaseTable {
protected int id;
protected Date lastModifiedDate;
// ...
}
and
和
@Entity
@Table(name = "Person")
public class Person extends BaseTable implements Serializable{
@Id
@GeneratedValue
@Column(name = "id")
public getId()
{
return super.id;
}
setId(log id)
{
super.id = id;
}
@Column(name = "lastmodifieddate")
@Temporal(TemporalType.DATE)
public getLastModifiedDate()
{
return super.lastModifiedDate;
}
setLastModifiedDate(Date date)
{
super.lastModifiedDate = date;
}
public getName()
// ...
}
回答by Rehman
Try this out in BaseTable class
在 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;
}
where BaseTable class should have @MappedSuperclass. And there must be one generated date field in an entity.
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;
}
}