Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

java - How do I know the id before saving an object in jpa

I have a new object. I want to know id before saving it. Is it possible? Or there is another way for this? I am using jpa as orm and oracle as database.

@Id
@Basic(optional = false)
@Column(name = "ID", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "woTypeSeq")
private Long id;

I have a code field in my entity. If the user doesn't enter a value for the code field, I want to set code as entity's id. If I persist entity, of course i can get id and set code value as id, but this is extra query database.

I want to do something like that

if(entity.getCode()==null) {
   entity.setCode(entity.getId);
   jpaDao.saveOrUpdate(entity);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With a @GeneratedValue type id you can't know that value in advance (before actually writing it). However once you persist your Bean, the id field will be populated in that bean instance and you can obtain it without needing to do an extra query for it. In other words:

MyEntiry myEnt = new MyEntity(); //the id field is null now
entityManager.persist(myEnt);//the id field is populated in myEnt now
Long id = myEnt.getId();

Also, depending on how your EntityManager is configured, you might need to also first commit the transaction (manually) before you can get that id.

Update as per comment

If you want to intercept and do something to the entity before it is saved and/or updated, you can use JPA LifeCycle Listeners (if you're using JPA version 2): Handling JPA lifecycle event using listeners and callbacks.

Basically you can make a validate() method in your bean, annotate it with @PrePersist and @PreUpdate and do the validation in it (if code is empty set it to id's value)

Update per 2nd comment

Yes, I honestly just thought of that just now: that if the id is auto generated, it might get populated AFTER the pre-persist event, such that when your pre-persist code is executed you still don't know what the id is (you may notice also that in the example you link to the id is NOT autogenerated but set manually). What you can do in this case is add a boolean field to your entity (annotated with @Transient so it won't get persisted) called isCodeEmpty (which is false by default if not specifically initialized). Then in your @PrePersist annotated method you check if the value for code field is empty and if so, set the boolean to true. Then you refactor your setId(...) method such that (aside from setting the id field) it will check this boolean, and if true set the value of the code field to that of the id field:

public class YourEntity {

@Transient
private boolean isCodeEmpty;

public void setId(Whatever id) {
 this.id = id;
 if(isCodeEmpty) {
  this.code = id;
  //if necessary:
  //this.isCodeEmpty = false;
 }
}

@PrePersist
public void validate() {
 if(code == null || code.isEmpty()) {
  isCodeEmpty = true;
 }

}


}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...