4

I'm using spring data (jpaRepository) + Oracle 11g Database.

Here's the code of my JUnit test:

@Test
    public void testAjoutUtilisateur() {
    Utilisateur utilisateur = new Utilisateur();

(...)

    utilisateur=repository.save(utilisateur);

    Utilisateur dbutilisateur = repository.findOne(utilisateur.getIdutilisateur());
    assertNotNull(dbutilisateur);

When I debug I find that "utilisateur" object returned by repository.save method has an id like "2100" while the corresponding inserted line in the database have an id like "43".

I have an Oracle database with a sequence and a trigger to have the auto incremented property for the id for my "Utilisateur" table.

Here is the id definition in my Utilisateur entity:

@Entity
@NamedQuery(name="Utilisateur.findAll", query="SELECT u FROM Utilisateur u")
@SequenceGenerator(sequenceName="ID_UTILISATEUR_SEQ", name="ID_UTILISATEUR_SEQ")
public class Utilisateur implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ID_UTILISATEUR_SEQ")
private Long idutilisateur;

Where is the problem? Is it within the save method?

Thank you.


Edit:

I figured out that the problem was already solved by the solution of @jhadesdev and the data lines I was talking about were inserted when the triggers were actives.

Finally, I have to mention that by default the JUnit test seems to not insert data in the database (it inserts then rollback). In order to invalidate this behaviour we have to specify the @TransactionConfiguration(defaultRollback=false) annotation in the test class.

For example (in my case):

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:context/dao-context.xml" })
@TransactionConfiguration(defaultRollback=false)
@Transactional
public class UtilisateurRepositoryTest {

Hope it can help someone.

Sinda MOKADDEM
  • 796
  • 2
  • 12
  • 35

1 Answers1

8

The problem is that two separate mechanisms are in place to generate the key:

  • one at Hibernate level which is to call a sequence and use the value to populate an Id column and send it to the database as the insert key
  • and another mechanism at the database that Hibernate does not know about: the column is incremented via a trigger.

Hibernate thinks that the insert was made with the value of the sequence, but in the database something else occurred. The simplest solution would probably be to remove the trigger mechanism, and let Hibernate populate the key based on the sequence only.

Angular University
  • 42,341
  • 15
  • 74
  • 81
  • Thank you @jhadesdev for your reply... I see the problem, I'll remove the trigger right now and give feedback – Sinda MOKADDEM Apr 10 '14 at 20:08
  • But why the value of the sequence is around 2000 ? while the sequence in my database has value around 50 now ? – Sinda MOKADDEM Apr 10 '14 at 20:19
  • Someone has an idea? why am I finding an id of 2100 or 3100 in the database even if the sequence starts with 1 and increments by 1 ?? – Sinda MOKADDEM Apr 15 '14 at 11:29
  • 1
    I finally found out that the problem was due to the sequence incrementing by 20 ! I don't know how it was set to 20 but when I checked, that's what I found. So, I've dropped the sequence and recreated it like that: `CREATE SEQUENCE sequenceName START WITH 1 INCREMENT BY 1 NOCACHE NOCYCLE;` – Sinda MOKADDEM Apr 25 '14 at 17:09
  • This answer has ended hours of headache for me ;-) . This post was also useful for me: http://stackoverflow.com/a/8003294/2507819 . It demonstrates how one can keep the trigger in this context. – JanTheGun Dec 03 '15 at 09:59