4

How can I implement mysql replace into using hibernate? I couldn't find this anywhere.

2
  • Sorry i am not sure about the mysql replace, but can you elaborate more on whats your requirement? What is it that you want to do? Commented Jun 17, 2013 at 15:30
  • 1
    in one of my project , I am having a number of update statements. it is taking so much time to execute. so I was planning to do a replace into instead of update. don't know how to do this through hibernate. Commented Jun 17, 2013 at 15:41

2 Answers 2

2

I believe, being an mySQL extension behavior, it's not supported directly. As normal for all unsupported query syntax's, you may want to use native SQL in this case.

Reference for native SQL support in hibernate, could be found here.

Sign up to request clarification or add additional context in comments.

Comments

1

Actually you can use Hibernated named queries easily. You can either use Hibernate mapping files or Hibernate annotations in your entity class to get the job done.

Using mapping files.

<hibernate-mapping>
    ......
    <sql-query name="yourNamedReplaceQuery">
         <![CDATA[REPLACE INTO T SELECT * FROM T]]>
    </sql-query>
</hibernate-mapping>

Using annotations

@NamedQueries({
    @NamedNativeQuery(
    name = "yourNamedReplaceQuery",
    query = "REPLACE INTO T SELECT * FROM T"
    )
})
@Entity
@Table(name = "your_object", catalog = "test")
public class YourObject implements java.io.Serializable {

Invoking the Named Query.

Query query = session.getNamedQuery("findStockByStockCodeNativeSQL");

More reference : http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.