Unable to generate constraint violation exception when testing Hibernate @Check constraints
up vote
1
down vote
favorite
I am playing with Hibernate @Check
annotation but can not have my test fail when constraints are not satisfied. Currently just using default Spring boot configuration with H2 database.
What am I missing? Should there be a some kind of a flush after save(..)
?
When running test I see the table created correctly. If I copy the creation line from the log and using it create a table to my 'real' Postgres database I can test different inserts and see that this line is all fine with constraints.
Entity
@Getter @Setter
@Entity @Check(constraints = "a IS NOT NULL OR b IS NOT NULL")
public class Constrained
@Id @GeneratedValue
private Long id;
private String a, b;
Test
@DataJpaTest
@RunWith(SpringRunner.class)
public class HibernateCheckTest
@Resource // this repo is just some boiler plate code but attached at
// the bottom of question
private ConstrainedRepository repo;
@Test @Transactional // also tried without @Transactional
public void test()
Constrained c = new Constrained();
repo.save(c); // Am I wrong to expect some constraint exception here?
Table generation script while running test
create table constrained (id bigint not null, a varchar(255), b
varchar(255), primary key (id), check (a IS NOT NULL OR b IS NOT
NULL))
Repository (not much to see in repo but just to show it):
public interface ConstrainedRepository
extends CrudRepository<Constrained, Long>
HOWEVER
If I use EntityManager
so adding to my test class:
@PersistenceContext
private EntityManager em;
and doing the persist like:
em.persist(c);
em.flush();
instead of repo.save(c)
I will get the exception.
AND
studying the log from the original test with repo.save(c)
more careful shows:
org.springframework.test.context.transaction.TransactionContext:139 - Rolled back transaction for test:
...
testException = [null],
so for some reason this error is just wrapped and logged. How to have it "unwrapped" and thrown when using repository for persisting?
java hibernate spring-boot jpa h2
add a comment |
up vote
1
down vote
favorite
I am playing with Hibernate @Check
annotation but can not have my test fail when constraints are not satisfied. Currently just using default Spring boot configuration with H2 database.
What am I missing? Should there be a some kind of a flush after save(..)
?
When running test I see the table created correctly. If I copy the creation line from the log and using it create a table to my 'real' Postgres database I can test different inserts and see that this line is all fine with constraints.
Entity
@Getter @Setter
@Entity @Check(constraints = "a IS NOT NULL OR b IS NOT NULL")
public class Constrained
@Id @GeneratedValue
private Long id;
private String a, b;
Test
@DataJpaTest
@RunWith(SpringRunner.class)
public class HibernateCheckTest
@Resource // this repo is just some boiler plate code but attached at
// the bottom of question
private ConstrainedRepository repo;
@Test @Transactional // also tried without @Transactional
public void test()
Constrained c = new Constrained();
repo.save(c); // Am I wrong to expect some constraint exception here?
Table generation script while running test
create table constrained (id bigint not null, a varchar(255), b
varchar(255), primary key (id), check (a IS NOT NULL OR b IS NOT
NULL))
Repository (not much to see in repo but just to show it):
public interface ConstrainedRepository
extends CrudRepository<Constrained, Long>
HOWEVER
If I use EntityManager
so adding to my test class:
@PersistenceContext
private EntityManager em;
and doing the persist like:
em.persist(c);
em.flush();
instead of repo.save(c)
I will get the exception.
AND
studying the log from the original test with repo.save(c)
more careful shows:
org.springframework.test.context.transaction.TransactionContext:139 - Rolled back transaction for test:
...
testException = [null],
so for some reason this error is just wrapped and logged. How to have it "unwrapped" and thrown when using repository for persisting?
java hibernate spring-boot jpa h2
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am playing with Hibernate @Check
annotation but can not have my test fail when constraints are not satisfied. Currently just using default Spring boot configuration with H2 database.
What am I missing? Should there be a some kind of a flush after save(..)
?
When running test I see the table created correctly. If I copy the creation line from the log and using it create a table to my 'real' Postgres database I can test different inserts and see that this line is all fine with constraints.
Entity
@Getter @Setter
@Entity @Check(constraints = "a IS NOT NULL OR b IS NOT NULL")
public class Constrained
@Id @GeneratedValue
private Long id;
private String a, b;
Test
@DataJpaTest
@RunWith(SpringRunner.class)
public class HibernateCheckTest
@Resource // this repo is just some boiler plate code but attached at
// the bottom of question
private ConstrainedRepository repo;
@Test @Transactional // also tried without @Transactional
public void test()
Constrained c = new Constrained();
repo.save(c); // Am I wrong to expect some constraint exception here?
Table generation script while running test
create table constrained (id bigint not null, a varchar(255), b
varchar(255), primary key (id), check (a IS NOT NULL OR b IS NOT
NULL))
Repository (not much to see in repo but just to show it):
public interface ConstrainedRepository
extends CrudRepository<Constrained, Long>
HOWEVER
If I use EntityManager
so adding to my test class:
@PersistenceContext
private EntityManager em;
and doing the persist like:
em.persist(c);
em.flush();
instead of repo.save(c)
I will get the exception.
AND
studying the log from the original test with repo.save(c)
more careful shows:
org.springframework.test.context.transaction.TransactionContext:139 - Rolled back transaction for test:
...
testException = [null],
so for some reason this error is just wrapped and logged. How to have it "unwrapped" and thrown when using repository for persisting?
java hibernate spring-boot jpa h2
I am playing with Hibernate @Check
annotation but can not have my test fail when constraints are not satisfied. Currently just using default Spring boot configuration with H2 database.
What am I missing? Should there be a some kind of a flush after save(..)
?
When running test I see the table created correctly. If I copy the creation line from the log and using it create a table to my 'real' Postgres database I can test different inserts and see that this line is all fine with constraints.
Entity
@Getter @Setter
@Entity @Check(constraints = "a IS NOT NULL OR b IS NOT NULL")
public class Constrained
@Id @GeneratedValue
private Long id;
private String a, b;
Test
@DataJpaTest
@RunWith(SpringRunner.class)
public class HibernateCheckTest
@Resource // this repo is just some boiler plate code but attached at
// the bottom of question
private ConstrainedRepository repo;
@Test @Transactional // also tried without @Transactional
public void test()
Constrained c = new Constrained();
repo.save(c); // Am I wrong to expect some constraint exception here?
Table generation script while running test
create table constrained (id bigint not null, a varchar(255), b
varchar(255), primary key (id), check (a IS NOT NULL OR b IS NOT
NULL))
Repository (not much to see in repo but just to show it):
public interface ConstrainedRepository
extends CrudRepository<Constrained, Long>
HOWEVER
If I use EntityManager
so adding to my test class:
@PersistenceContext
private EntityManager em;
and doing the persist like:
em.persist(c);
em.flush();
instead of repo.save(c)
I will get the exception.
AND
studying the log from the original test with repo.save(c)
more careful shows:
org.springframework.test.context.transaction.TransactionContext:139 - Rolled back transaction for test:
...
testException = [null],
so for some reason this error is just wrapped and logged. How to have it "unwrapped" and thrown when using repository for persisting?
java hibernate spring-boot jpa h2
java hibernate spring-boot jpa h2
edited yesterday
asked yesterday
pirho
3,355101730
3,355101730
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
In ConstrainedRepository
, extend JpaRepository
instead of CrudRepository
then use:
repo.saveAndFlush(c);
instead of:
repo.save(c);
The check is enforced in the database and that only happens when the changes (in this case an INSERT statement) are flushed to the database.
Without an explicit flush, Hibernate will defer sending the statements to the database until the transaction is committed or a query is executed.
However, from the Spring DataJpaTest documentation:
By default, data JPA tests are transactional and roll back at the end
of each test.
So, in this case, there is not commit. The transaction is rolled back and the statements are never flushed to the database, and therefore the exception is never thrown.
Thanks. I would have assumed something like that. I have seen this method in some Q&As but my problem now is that my repos do not contain that method? Should i construct myt repo or test somehow differently? (i'm browsing the docs also but if you know quick answer)
– pirho
yesterday
Ah, sorry that method is in JpaRepository but not CrudRepository. Have updated my answer.
– codemonkey
yesterday
add a comment |
up vote
1
down vote
Thanks to the answer from codemonkey I was able to find a solution. This resolves by adding:
@org.springframework.transaction.annotation.Transactional(propagation =
Propagation.NOT_SUPPORTED)
to my test class.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
In ConstrainedRepository
, extend JpaRepository
instead of CrudRepository
then use:
repo.saveAndFlush(c);
instead of:
repo.save(c);
The check is enforced in the database and that only happens when the changes (in this case an INSERT statement) are flushed to the database.
Without an explicit flush, Hibernate will defer sending the statements to the database until the transaction is committed or a query is executed.
However, from the Spring DataJpaTest documentation:
By default, data JPA tests are transactional and roll back at the end
of each test.
So, in this case, there is not commit. The transaction is rolled back and the statements are never flushed to the database, and therefore the exception is never thrown.
Thanks. I would have assumed something like that. I have seen this method in some Q&As but my problem now is that my repos do not contain that method? Should i construct myt repo or test somehow differently? (i'm browsing the docs also but if you know quick answer)
– pirho
yesterday
Ah, sorry that method is in JpaRepository but not CrudRepository. Have updated my answer.
– codemonkey
yesterday
add a comment |
up vote
2
down vote
In ConstrainedRepository
, extend JpaRepository
instead of CrudRepository
then use:
repo.saveAndFlush(c);
instead of:
repo.save(c);
The check is enforced in the database and that only happens when the changes (in this case an INSERT statement) are flushed to the database.
Without an explicit flush, Hibernate will defer sending the statements to the database until the transaction is committed or a query is executed.
However, from the Spring DataJpaTest documentation:
By default, data JPA tests are transactional and roll back at the end
of each test.
So, in this case, there is not commit. The transaction is rolled back and the statements are never flushed to the database, and therefore the exception is never thrown.
Thanks. I would have assumed something like that. I have seen this method in some Q&As but my problem now is that my repos do not contain that method? Should i construct myt repo or test somehow differently? (i'm browsing the docs also but if you know quick answer)
– pirho
yesterday
Ah, sorry that method is in JpaRepository but not CrudRepository. Have updated my answer.
– codemonkey
yesterday
add a comment |
up vote
2
down vote
up vote
2
down vote
In ConstrainedRepository
, extend JpaRepository
instead of CrudRepository
then use:
repo.saveAndFlush(c);
instead of:
repo.save(c);
The check is enforced in the database and that only happens when the changes (in this case an INSERT statement) are flushed to the database.
Without an explicit flush, Hibernate will defer sending the statements to the database until the transaction is committed or a query is executed.
However, from the Spring DataJpaTest documentation:
By default, data JPA tests are transactional and roll back at the end
of each test.
So, in this case, there is not commit. The transaction is rolled back and the statements are never flushed to the database, and therefore the exception is never thrown.
In ConstrainedRepository
, extend JpaRepository
instead of CrudRepository
then use:
repo.saveAndFlush(c);
instead of:
repo.save(c);
The check is enforced in the database and that only happens when the changes (in this case an INSERT statement) are flushed to the database.
Without an explicit flush, Hibernate will defer sending the statements to the database until the transaction is committed or a query is executed.
However, from the Spring DataJpaTest documentation:
By default, data JPA tests are transactional and roll back at the end
of each test.
So, in this case, there is not commit. The transaction is rolled back and the statements are never flushed to the database, and therefore the exception is never thrown.
edited yesterday
answered yesterday
codemonkey
1,3253717
1,3253717
Thanks. I would have assumed something like that. I have seen this method in some Q&As but my problem now is that my repos do not contain that method? Should i construct myt repo or test somehow differently? (i'm browsing the docs also but if you know quick answer)
– pirho
yesterday
Ah, sorry that method is in JpaRepository but not CrudRepository. Have updated my answer.
– codemonkey
yesterday
add a comment |
Thanks. I would have assumed something like that. I have seen this method in some Q&As but my problem now is that my repos do not contain that method? Should i construct myt repo or test somehow differently? (i'm browsing the docs also but if you know quick answer)
– pirho
yesterday
Ah, sorry that method is in JpaRepository but not CrudRepository. Have updated my answer.
– codemonkey
yesterday
Thanks. I would have assumed something like that. I have seen this method in some Q&As but my problem now is that my repos do not contain that method? Should i construct myt repo or test somehow differently? (i'm browsing the docs also but if you know quick answer)
– pirho
yesterday
Thanks. I would have assumed something like that. I have seen this method in some Q&As but my problem now is that my repos do not contain that method? Should i construct myt repo or test somehow differently? (i'm browsing the docs also but if you know quick answer)
– pirho
yesterday
Ah, sorry that method is in JpaRepository but not CrudRepository. Have updated my answer.
– codemonkey
yesterday
Ah, sorry that method is in JpaRepository but not CrudRepository. Have updated my answer.
– codemonkey
yesterday
add a comment |
up vote
1
down vote
Thanks to the answer from codemonkey I was able to find a solution. This resolves by adding:
@org.springframework.transaction.annotation.Transactional(propagation =
Propagation.NOT_SUPPORTED)
to my test class.
add a comment |
up vote
1
down vote
Thanks to the answer from codemonkey I was able to find a solution. This resolves by adding:
@org.springframework.transaction.annotation.Transactional(propagation =
Propagation.NOT_SUPPORTED)
to my test class.
add a comment |
up vote
1
down vote
up vote
1
down vote
Thanks to the answer from codemonkey I was able to find a solution. This resolves by adding:
@org.springframework.transaction.annotation.Transactional(propagation =
Propagation.NOT_SUPPORTED)
to my test class.
Thanks to the answer from codemonkey I was able to find a solution. This resolves by adding:
@org.springframework.transaction.annotation.Transactional(propagation =
Propagation.NOT_SUPPORTED)
to my test class.
answered yesterday
pirho
3,355101730
3,355101730
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237507%2funable-to-generate-constraint-violation-exception-when-testing-hibernate-check%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password