Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java hibernate注释-Inheritation with@MappedSuperclass-未将值设置为基类字段-奇怪的错误_Java_Hibernate_Inheritance_Jpa_Annotations - Fatal编程技术网

Java hibernate注释-Inheritation with@MappedSuperclass-未将值设置为基类字段-奇怪的错误

Java hibernate注释-Inheritation with@MappedSuperclass-未将值设置为基类字段-奇怪的错误,java,hibernate,inheritance,jpa,annotations,Java,Hibernate,Inheritance,Jpa,Annotations,我正在跟踪并将ID、lastModifiedDate等公共列放在基表中 我的注释映射如下所示: 基本表: @MappedSuperclass public abstract class BaseTable { @Id @GeneratedValue @Column(name = "id") private int id; @Column(name = "lastmodifieddate") private Date lastModifiedDat

我正在跟踪并将ID、lastModifiedDate等公共列放在基表中

我的注释映射如下所示:

基本表:

@MappedSuperclass
public abstract class BaseTable {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "lastmodifieddate")
    private Date lastModifiedDate;
...
create table Person (id integer not null auto_increment,  lastmodifieddate datetime, name varchar(255), primary key (id)) ; 
insert into Person (lastmodifieddate, name) values (?, ?)
binding parameter [1] as [TIMESTAMP] - <null>
binding parameter [2] as [VARCHAR] - Test
Query query = getSession().createQuery("from " + Person.class.getName());
List<Person> allPersons=query.list();
人员表-

    @Entity
    @Table(name = "Person ")
    public class Person extends BaseTable  implements Serializable{

    @Column(name = "name")
    private String name;
...
生成的Create语句:

@MappedSuperclass
public abstract class BaseTable {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "lastmodifieddate")
    private Date lastModifiedDate;
...
create table Person (id integer not null auto_increment,  lastmodifieddate datetime, name varchar(255), primary key (id)) ; 
insert into Person (lastmodifieddate, name) values (?, ?)
binding parameter [1] as [TIMESTAMP] - <null>
binding parameter [2] as [VARCHAR] - Test
Query query = getSession().createQuery("from " + Person.class.getName());
List<Person> allPersons=query.list();
将Person对象保存到db后

Person p = new Person();
p.setName("Test");
p.setLastModifiedDate(new Date());
..

getSession().save(p);
我正在设置日期字段,但它正在使用生成的ID和LastModifiedDate=null以及Name=“Test”保存记录

插入语句:

@MappedSuperclass
public abstract class BaseTable {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "lastmodifieddate")
    private Date lastModifiedDate;
...
create table Person (id integer not null auto_increment,  lastmodifieddate datetime, name varchar(255), primary key (id)) ; 
insert into Person (lastmodifieddate, name) values (?, ?)
binding parameter [1] as [TIMESTAMP] - <null>
binding parameter [2] as [VARCHAR] - Test
Query query = getSession().createQuery("from " + Person.class.getName());
List<Person> allPersons=query.list();
ReadAll查询:

@MappedSuperclass
public abstract class BaseTable {
    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @Column(name = "lastmodifieddate")
    private Date lastModifiedDate;
...
create table Person (id integer not null auto_increment,  lastmodifieddate datetime, name varchar(255), primary key (id)) ; 
insert into Person (lastmodifieddate, name) values (?, ?)
binding parameter [1] as [TIMESTAMP] - <null>
binding parameter [2] as [VARCHAR] - Test
Query query = getSession().createQuery("from " + Person.class.getName());
List<Person> allPersons=query.list();
但当我在控制台中打印列表时,它就更奇怪了。它正在选择带有对象的人员列表

  • ID字段=全部0(为什么全部为0?)
  • LastModifiedDate=null
  • 名称字段具有有效值
我不知道这里出了什么问题。你能看一下吗

供参考,

我的Hibernate核心版本:4.1.2,MySQL连接器版本:5.1.9

总之,这里有两个问题

  • 为什么使用“全部读取”时所有ID字段均为0
  • 为什么没有插入LastModifiedDate
使用

@Temporal(TemporalType.DATE)  
日期注释

对于ID生成使用策略

@GeneratedValue(strategy=GenerationType.AUTO) 

或者使用sequance

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "custom_generator")
@SequenceGenerator(name = "custom_generator", sequenceName = "sequance_table")  
您还可以在子类中进行属性访问:

public abstract class BaseTable {

    protected int id;

    protected Date lastModifiedDate;  
// ...  
}    


在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;
    }



}   

数据库中的“lastmodifieddate”是什么类型的?它的日期时间
datetime
。映射正在正确地生成DB表中的列。对于
id
,您使用的持久性提供程序是什么?在persistence.xml中。冬眠持续?对不起,我没听懂。我没有任何persistence.xml文件。我使用Spring+Hibernate和注释来映射持久对象。你能告诉我你所说的
持久性提供者是什么吗?我尝试了你建议的所有方法,但仍然是一样的。。谢谢。日期仍然没有插入?更新了,没有@MappedSuperclass,但想法相同。如果仍然不工作,那我就无能为力了。。。