1

I have a local server provided by GlassFish and I have 2 clients: one is generated by server: JSF; and the other one is not on the server: Terminal client written in Java.

Everything was working fine till this moment. Every time when I added a new row into a table, my auto incrementing Primary Key (numeric id) was finely raised by 1 and a row was added. Now, every time when I freshly start a client and I add a data row into a table via client (does not matter which one - both do the same mess), id is raised to the closest 51 (i.e. last id was 303, so after adding, the id of the new row in the table is 351). If my client is still running since the last addition and I add the next row, it works fine: the next id is 352. It continues without problems till I restart my client. When I add a new row after restarting client, it does the same stuff again: raising id to the closest 51 and then raising but 1.

Is this some kind of bug due to relation of Java and MySQL? This has never happened to me before when I used PHP.

EDIT: For manipulating with database I am using ORM technique via EclipseLink (QuestionDAO.java):

This is what I do in my data layer when I am adding a question:

public void add( Question q )
{
    em . persist(q);
}

This is what I do in my business layer when I am calling a method from data layer (Facade.java):

public String addQuestion( String text, String subject )
{
    Subject s = subjectDAO.find( subject );
    if( s != null )
    {
        Question q = new Question( text, s );
        questionDAO . add(q);
        return "Question successfully added";
    }
    else
        return "Subject does not exist";
}

Also here is a method from REST:

@POST
@Path("add")
public String add( @FormParam("text") String text, @FormParam("subject") String subject )
{
    String s = facade.addQuestion(text, subject);
    return s;
}

Annotations in my Entity class:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Basic(optional = false)
@Column(name = "question_number")
private Integer questionNumber;

I do not believe that a mistake is in my code yet.

scarface
  • 574
  • 6
  • 20
  • *"Is this some kind of bug due to relation of Java and MySQL?"* Of course not. Remember the rule of thumb: The problem is **much** more likely to be in your code and setup than in a thoroughly tested tool. Or as the Pragmtic Programmer put it: [`select` isn't broken](https://pragprog.com/the-pragmatic-programmer/extracts/tips). But we can't help you find the problem in your code or setup without a **lot** more information (if then; frankly, this is one of those things that you just have to debug in place, which aren't usually a good fit for Q&A site). – T.J. Crowder Jan 09 '17 at 09:47
  • First, check if you are really using the autoincrement (if you didn't set a value in the insert query), then check if the same insert query work using command line or a client (like PhpMySql or other). – AxelH Jan 09 '17 at 09:55
  • I just tried to add 2 rows in phpMyAdmin and everything worked fine. – scarface Jan 09 '17 at 09:56
  • Possible duplicate of [How to annotate MYSQL autoincrement field with JPA annotations](http://stackoverflow.com/questions/4102449/how-to-annotate-mysql-autoincrement-field-with-jpa-annotations) – AxelH Jan 09 '17 at 09:57
  • I have edited a question, maybe it will give you some info. – scarface Jan 09 '17 at 09:57
  • I have the same annotations. Adding them into a post – scarface Jan 09 '17 at 09:58
  • 1
    Well, I would tried to remove the `@Basic` to be sure that it is not overriding the rest of the annotation. `@id` should be enough here to show the necessity. Also, read the comments in the answer. I would agree to be verbose on `@GeneratedValue` by specifiying what you want – AxelH Jan 09 '17 at 10:02
  • Okay, thank you. Gonna try that when I am at pc again. I will let you know. – scarface Jan 09 '17 at 11:09
  • Perfect, I just changed from `@GeneratedValue(strategy=GenerationType.AUTO)` to `@GeneratedValue(strategy=GenerationType.IDENTITY)` and it is working again now. Thank you – scarface Jan 09 '17 at 23:06

0 Answers0