Skip to content

Commit 0ff8360

Browse files
committed
committed initial Hibernate 4.0 support
1 parent 55f91e5 commit 0ff8360

18 files changed

+2197
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.orm.hibernate4;
18+
19+
import org.hibernate.HibernateException;
20+
21+
import org.springframework.dao.DataAccessException;
22+
import org.springframework.dao.support.PersistenceExceptionTranslator;
23+
24+
/**
25+
* {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException}
26+
* instances to Spring's {@link org.springframework.dao.DataAccessException} hierarchy.
27+
*
28+
* @author Juergen Hoeller
29+
* @since 3.1
30+
* @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
31+
* @see SessionFactoryUtils#convertHibernateAccessException(HibernateException)
32+
*/
33+
public class HibernateExceptionTranslator implements PersistenceExceptionTranslator {
34+
35+
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
36+
if (ex instanceof HibernateException) {
37+
return convertHibernateAccessException((HibernateException) ex);
38+
}
39+
return null;
40+
}
41+
42+
/**
43+
* Convert the given HibernateException to an appropriate exception from the
44+
* {@code org.springframework.dao} hierarchy.
45+
* @param ex HibernateException that occured
46+
* @return a corresponding DataAccessException
47+
* @see SessionFactoryUtils#convertHibernateAccessException
48+
*/
49+
protected DataAccessException convertHibernateAccessException(HibernateException ex) {
50+
return SessionFactoryUtils.convertHibernateAccessException(ex);
51+
}
52+
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.orm.hibernate4;
18+
19+
import java.sql.SQLException;
20+
21+
import org.hibernate.JDBCException;
22+
23+
import org.springframework.dao.UncategorizedDataAccessException;
24+
25+
/**
26+
* Hibernate-specific subclass of UncategorizedDataAccessException,
27+
* for JDBC exceptions that Hibernate wrapped.
28+
*
29+
* @author Juergen Hoeller
30+
* @since 3.1
31+
* @see SessionFactoryUtils#convertHibernateAccessException
32+
*/
33+
public class HibernateJdbcException extends UncategorizedDataAccessException {
34+
35+
public HibernateJdbcException(JDBCException ex) {
36+
super("JDBC exception on Hibernate data access: SQLException for SQL [" + ex.getSQL() + "]; SQL state [" +
37+
ex.getSQLState() + "]; error code [" + ex.getErrorCode() + "]; " + ex.getMessage(), ex);
38+
}
39+
40+
/**
41+
* Return the underlying SQLException.
42+
*/
43+
public SQLException getSQLException() {
44+
return ((JDBCException) getCause()).getSQLException();
45+
}
46+
47+
/**
48+
* Return the SQL that led to the problem.
49+
*/
50+
public String getSql() {
51+
return ((JDBCException) getCause()).getSQL();
52+
}
53+
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.orm.hibernate4;
18+
19+
import org.hibernate.UnresolvableObjectException;
20+
import org.hibernate.WrongClassException;
21+
22+
import org.springframework.orm.ObjectRetrievalFailureException;
23+
24+
/**
25+
* Hibernate-specific subclass of ObjectRetrievalFailureException.
26+
* Converts Hibernate's UnresolvableObjectException and WrongClassException.
27+
*
28+
* @author Juergen Hoeller
29+
* @since 3.1
30+
* @see SessionFactoryUtils#convertHibernateAccessException
31+
*/
32+
public class HibernateObjectRetrievalFailureException extends ObjectRetrievalFailureException {
33+
34+
public HibernateObjectRetrievalFailureException(UnresolvableObjectException ex) {
35+
super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
36+
}
37+
38+
public HibernateObjectRetrievalFailureException(WrongClassException ex) {
39+
super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
40+
}
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.orm.hibernate4;
18+
19+
import org.hibernate.StaleObjectStateException;
20+
import org.hibernate.StaleStateException;
21+
22+
import org.springframework.orm.ObjectOptimisticLockingFailureException;
23+
24+
/**
25+
* Hibernate-specific subclass of ObjectOptimisticLockingFailureException.
26+
* Converts Hibernate's StaleObjectStateException and StaleStateException.
27+
*
28+
* @author Juergen Hoeller
29+
* @since 3.1
30+
* @see SessionFactoryUtils#convertHibernateAccessException
31+
*/
32+
public class HibernateOptimisticLockingFailureException extends ObjectOptimisticLockingFailureException {
33+
34+
public HibernateOptimisticLockingFailureException(StaleObjectStateException ex) {
35+
super(ex.getEntityName(), ex.getIdentifier(), ex);
36+
}
37+
38+
public HibernateOptimisticLockingFailureException(StaleStateException ex) {
39+
super(ex.getMessage(), ex);
40+
}
41+
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.orm.hibernate4;
18+
19+
import org.hibernate.QueryException;
20+
21+
import org.springframework.dao.InvalidDataAccessResourceUsageException;
22+
23+
/**
24+
* Hibernate-specific subclass of InvalidDataAccessResourceUsageException,
25+
* thrown on invalid HQL query syntax.
26+
*
27+
* @author Juergen Hoeller
28+
* @since 3.1
29+
* @see SessionFactoryUtils#convertHibernateAccessException
30+
*/
31+
public class HibernateQueryException extends InvalidDataAccessResourceUsageException {
32+
33+
public HibernateQueryException(QueryException ex) {
34+
super(ex.getMessage(), ex);
35+
}
36+
37+
/**
38+
* Return the HQL query string that was invalid.
39+
*/
40+
public String getQueryString() {
41+
return ((QueryException) getCause()).getQueryString();
42+
}
43+
44+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.orm.hibernate4;
18+
19+
import org.hibernate.HibernateException;
20+
21+
import org.springframework.dao.UncategorizedDataAccessException;
22+
23+
/**
24+
* Hibernate-specific subclass of UncategorizedDataAccessException,
25+
* for Hibernate system errors that do not match any concrete
26+
* <code>org.springframework.dao</code> exceptions.
27+
*
28+
* @author Juergen Hoeller
29+
* @since 3.1
30+
* @see SessionFactoryUtils#convertHibernateAccessException
31+
*/
32+
public class HibernateSystemException extends UncategorizedDataAccessException {
33+
34+
/**
35+
* Create a new HibernateSystemException,
36+
* wrapping an arbitrary HibernateException.
37+
* @param cause the HibernateException thrown
38+
*/
39+
public HibernateSystemException(HibernateException cause) {
40+
super(cause != null ? cause.getMessage() : null, cause);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)