-1

I make a project like this: https://grokonez.com/spring-framework/spring-boot/use-spring-jpa-mysql-spring-boot and run normally

But when I change "id" with column(name="id") in MYSQL . I have an error when the test on the postman.

{
"timestamp": 1533183310810,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.transaction.UnexpectedRollbackException",
"message": "JTA transaction unexpectedly rolled back (maybe due to a timeout); nested exception is javax.transaction.RollbackException: ARJUNA016053: Could not commit transaction.",
"path": "/save"
}

I change something class Customer :

@Id
@Column(name="id")
private long id;

public Customer(long id , String firstName, String lastName){
    this.id=id;
    this.firstName=firstName;
    this.lastName=lastName;
}

In class WebController, I save a customer with id, first name, last name

@RequestMapping("/save")
public String process(){
    repository.save(new Customer(1,"Jack", "Smith"));
    return "Done";
}

How to save a customer with id?

Mr. Huynh
  • 1
  • 1
  • https://stackoverflow.com/questions/35356742/cant-commit-jpa-transaction-rollbackexception-transaction-marked-as-rollback/35377970#35377970 – Pallav Jha Aug 02 '18 at 04:28
  • When debug , I see error : "ARJUNA016060: TransactionImple.enlistResource - caught: XAException.XAER_INVAL" – Mr. Huynh Aug 02 '18 at 04:44
  • ConnectionImple.registerDatabase - ARJUNA017017: enlist of resource failed – Mr. Huynh Aug 02 '18 at 04:46
  • and ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for SynchronizationImple< 0:ffffac10043e:cfd4:5b628ab4:18, org.hibernate.resource.transaction.backend.jta.internal.synchronization.RegisteredSynchronization@3899e7d6 > – Mr. Huynh Aug 02 '18 at 04:46
  • 2
    Add the stacktrace to your question please – Jens Aug 02 '18 at 04:49
  • In the code you base on, there is a create mysql table part where id column is defined as autoincrement. Maybe this mismatch cause an exception. – piradian Aug 02 '18 at 04:57

2 Answers2

0

In that article, they are using GenerationType.AUTO strategy for Id column. This means that value in ID column will be generated automatically. So, to save a record, you don't need to explicitly provide an ID.

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

Save should be like this:

repository.save(new Customer("Jack", "Smith"));

Benefit of this is that you don't need to care if the ID you're going to assign is already been assigned to any other record. JPA takes care of assigning a new ID to all new records.

Vijay Nandwana
  • 2,476
  • 4
  • 25
  • 42
  • I know this way . I think if in database , if I have 6 row with id from 1 - 6 and I delete the row where id=1 . When I insert into my database , if I use @GeneratedValue - the id now is 7 but I want the id = 1 . So that's why I ask this question – Mr. Huynh Aug 03 '18 at 03:36
  • Please mention this in your question at the top. – Vijay Nandwana Aug 03 '18 at 06:19
0

you might be getting this error because the id you are trying to insert might be in DB already. If you try inserting data with different id your code will work.

Though we generally follow the approach shared above(@GeneratedValue(strategy = GenerationType.AUTO)).

  • I know this way . I think if in database , if I have 6 row with id from 1 - 6 and I delete the row where id=1 . When I insert into my database , if I use @GeneratedValue - the id now is 7 but I want the id = 1 . So that's why I ask this question. – Mr. Huynh Aug 03 '18 at 03:36
  • ID is not already in my DB but when I insert from Spring boot . I have error .... – Mr. Huynh Aug 03 '18 at 03:36