Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - Force Hibernate Insert Without Select Statements

I am attempting to insert a new record into a table that I know is unique before hand. I have tried calling save() on the object, but that does a bunch of SELECT statements before doing any INSERTs, which I don't want to do because I know the object is already unique.

I am opening a new session for each transaction, which I can see being an issue, but that is a constraint of my domain. Is there some way to force Hibernate to not do any SELECTs before it INSERTs?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You can use the persist() method rather than save().

https://forum.hibernate.org/viewtopic.php?f=1&t=1011405

However, unlike save(), persist() does not guarantee that the identifier value will be set immediately on the persisted instance.

https://forum.hibernate.org/viewtopic.php?f=1&t=951275

(and you can jump to christian's last post in the thread)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...