Hey guys,
I got a little problem with database synchronisation using JPA.
I defined a Entity like the following example:
@Entity
@Table(name = "LOCATION")
public class Location implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name="location_gen", sequenceName="SEQ_LOCATION_ID")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="location_gen")
@Basic(optional = false)
@Column(name = "LOCATION_ID")
private long locationId;
@Basic(optional = false)
@Column(name = "LOCATION_TYPE")
private char LocationType;
@Column(name = "LOCATION")
private String location;
//....
}
There is a simple LocationJpaController like this:
public class LocationJpaController{
public LocationJpaController() {
emf = Persistence.createEntityManagerFactory("unit12345");
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Location location) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(location);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
}
in my main-method I just create a new Location-Object wihtout an ID and store it via the create-method of LocationJpaController to the database.
LocationJpaController controller= new LocationJpaController ();
Location loc = new Location();
loc.setLocation("My Location 1234");
loc.setType('A');
controller.create(loc);
After this, the loc object has an ID. The problem is, that the id which is in the database and the refering id in the created location object loc are different.
For example, loc.getId() returns 10 and in the database is 60 stored.
But I do not know why they are different. Can you please help me up?