0

I am connecting to Postgres database using hibernate. In the database there is a table, where one of the column is set to store current time when the record is inserted in that table. The current time is automatically populated when I insert record from Postgres interface.

But when I try to insert the record from Hibernate, record is not inserted in the current time column by the database automatically.

Query dateQuery=session.createQuery("select b.boilerPlateContent from Boiler_Plates b join b.bt_contracts c where c.contractId=:val order by b.boilerPlateContent desc)").setEntity("val",ct);
Iterator dateIterator = dateQuery.list().iterator();
String latestBoilerPlate=(String)dateIterator.next();
System.out.println(latestBoilerPlate);
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher(latestBoilerPlate);
while(m.find()){
 lastEntered=m.group();
 nextBoilerPlateNumber=Integer.parseInt(m.group());
}
nextBoilerPlateNumber++;
Boiler_Plates  bp=new Boiler_Plates();
bp.setBoiler_plate_id(boilerPlateId);
boilerPlateText="bp"+nextBoilerPlateNumber;
bp.setBoilerPlateContent(boilerPlateText);
bp.setBoilerPlateName("Test");
//bp.setInsertTime();
bp.setContract(ct);
session.save(bp);
tx.commit(); 
Alok
  • 244
  • 2
  • 8
  • 23

2 Answers2

2

You appear to be trying to do auditing. There are extremely well established solutions for this that you should use rather than rolling your own. See envers, trigger examples on the PostgreSQL wiki, and the JPA auditing support with @PrePersist, @PreUpdate, and entity listeners. Even better, use an @Embeddable entity and an @EntityListener so you can reuse your audit code.

You haven't specified how your column is automatically set.

If you've set a DEFAULT, well, Hibernate specifies values for all columns on an INSERT so the DEFAULT will be unused. You need to get Hibernate to avoid setting the column or explicitly specify the keyword DEFAULT as the column value - you can do this by mapping it as insertable=false,updatable=false. Alternately, you need to get Hibernate to insert the value you want directly.

Another option is to use an ON INSERT FOR EACH ROW trigger to set the value of the column. That lets you set the value from PL/PgSQL no matter what someone specifies for the column at INSERT time.

Here's another entity listener example.

Community
  • 1
  • 1
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • Your answer helped me solve the problem. I set the insert attribute in the property as false and it worked well. – Alok May 23 '12 at 14:32
0

As already pointed out, the information in your initial question is pretty lacking. But assuming that "The current time is automatically populated when I insert record" is meant to say that you have a DEFAULT defined on that column, DEFAULT values only take effect when that column is not referenced in the insert statement. Hibernate will reference all columns in the insert statement by default. However, you can change that behavior. Here you are looking for something like this mapping I think:

@Entity
public class Boiler_Plates {
    ...
    @Temporal(TemporalType.TIMESTAMP)
    @Generated(GenerationTime.INSERT) 
    @Column(insertable = false)
    Date insertTime
}

@Column(insertable = false) says to not include this column in the INSERT statement. @Generated(GenerationTime.INSERT) says to reread the state of that column after INSERT is executed to find the generated value.

Steve Ebersole
  • 9,339
  • 2
  • 48
  • 46