0

I was following Hibernate: Use a Base Class to Map Common Fields and openjpa inheritance tutorial to put common columns like ID, lastModifiedDate etc in base table.

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 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".

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.

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();

Corresponding SQL for read all

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

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 .

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?
4

2 回答 2

1

利用

@Temporal(TemporalType.DATE)  

注释Date

对于 ID 生成使用策略。

@GeneratedValue(strategy=GenerationType.AUTO) 

或者

@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;  
// ...  
}    

@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()
// ...
}
于 2012-06-16T16:26:02.290 回答
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;
    }



}   
于 2012-12-11T12:26:48.790 回答