Note that there are some explanatory texts on larger screens.

plurals
  1. PO@Id is not set automatically on insert in entity bean
    primarykey
    data
    text
    <p>I have a rather simple EJB application which consists of</p> <p><strong>One Entity Bean:</strong></p> <pre><code>import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Employee implements Serializable { private static final long serialVersionUID = -8450766960140252704L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="employee_number", nullable=false) private int employeeNumber; private String firstname; private String inital; private String lastname; private int age; private Date birthday; private String street; @Column(name="house_number") private String houseNumber; private String postalcode; private String city; private String department; @Column(name="sallery_group") private String salleryGroup; public int getEmployeeNumber() { return employeeNumber; } public void setEmployeeNumber(int employeeNumber) { this.employeeNumber = employeeNumber; } public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getInital() { return inital; } public void setInital(String inital) { this.inital = inital; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getHouseNumber() { return houseNumber; } public void setHouseNumber(String houseNumber) { this.houseNumber = houseNumber; } public String getPostalcode() { return postalcode; } public void setPostalcode(String postalcode) { this.postalcode = postalcode; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getSalleryGroup() { return salleryGroup; } public void setSalleryGroup(String salleryGroup) { this.salleryGroup = salleryGroup; } @Override public String toString() { StringBuffer sb = new StringBuffer(); sb.append("Employee Number: " + this.getEmployeeNumber() + "\n"); sb.append("Firstname: " + this.getFirstname() + "\n"); sb.append("Inital: " + this.getInital() + "\n"); sb.append("Lastname: " + this.getLastname() + "\n"); sb.append("Age: " + this.getAge() + "\n"); sb.append("Birthday: " + this.getBirthday() + "\n"); sb.append("Street: " + this.getStreet() + "\n"); sb.append("House Number: " + this.getHouseNumber() + "\n"); sb.append("Postalcode: " + this.getPostalcode() + "\n"); sb.append("City: " + this.getCity() + "\n"); sb.append("Department: " + this.getDepartment() + "\n"); sb.append("Sallery Group: " + this.getSalleryGroup() + "\n"); return sb.toString(); } @Override public boolean equals(Object obj) { if(obj == null || this.getClass() != obj.getClass()) { return false; } else if (obj == this) { return true; } else { Employee employee = (Employee)obj; return ( ((this.getFirstname() == null &amp;&amp; employee.getFirstname() == null) || (this.getFirstname().equals(employee.getFirstname()))) &amp;&amp; ((this.getInital() == null &amp;&amp; employee.getInital() == null) || (this.getInital().equals(employee.getInital()))) &amp;&amp; ((this.getLastname() == null &amp;&amp; employee.getLastname() == null) || (this.getLastname().equals(employee.getLastname()))) &amp;&amp; this.getAge() == employee.getAge() &amp;&amp; ((this.getBirthday() == null &amp;&amp; employee.getBirthday() == null) || (this.getBirthday().equals(employee.getBirthday()))) &amp;&amp; ((this.getStreet() == null &amp;&amp; employee.getStreet() == null) || (this.getStreet().equals(employee.getStreet()))) &amp;&amp; ((this.getHouseNumber() == null &amp;&amp; employee.getHouseNumber() == null) || (this.getHouseNumber().equals(employee.getHouseNumber()))) &amp;&amp; ((this.getPostalcode() == null &amp;&amp; employee.getPostalcode() == null) || (this.getPostalcode().equals(employee.getPostalcode()))) &amp;&amp; ((this.getCity() == null &amp;&amp; employee.getCity() == null) || (this.getCity().equals(employee.getCity()))) &amp;&amp; ((this.getDepartment() == null &amp;&amp; employee.getDepartment() == null) || (this.getDepartment().equals(employee.getDepartment()))) &amp;&amp; ((this.getSalleryGroup() == null &amp;&amp; employee.getSalleryGroup() == null) || (this.getSalleryGroup().equals(employee.getSalleryGroup())))); } } } </code></pre> <p><strong>One Session Bean</strong></p> <pre><code>import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @Stateless public class EmployeeManagementBean implements EmployeeManagement { @PersistenceContext(unitName="EmployeePersistenceUnit") private EntityManager entityManager; @Override public Employee create() { return new Employee(); } @Override public void store(Employee employee) { entityManager.persist(employee); } @Override public Employee findEmployeeWithNumber(int employeeNumber) { return entityManager.find(Employee.class, employeeNumber); } } </code></pre> <p>In my client I just call create() on the session bean, set all properties <strong>except</strong> the id field which is in my case employeeNumber. Afterwards I call store() and pass the object to the session bean. The object is then stored propperly in the database and the primary key is generated automatically by the mysql database.</p> <p><strong>Problem</strong></p> <p>My problem is, that the generated id is not passed back to the object. So after calling store() and therefore entityManager.persist() a call of getEmployeeNumber() allways returns 0. According to many things I read on the web the @Id field should be automatically be populated by the id generated by the database. Hopefully somebody can help me with that. entityManager.flush() doesn't solve the problem.</p> <p><strong>Further Details</strong></p> <p>I am using JBoss 7.1.0 and MySQL 5.6. MySQL JDBC Driver is put in deployment folder.</p> <p><strong>persistence.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt; &lt;persistence-unit name="EmployeePersistenceUnit" transaction-type="JTA"&gt; &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt; &lt;jta-data-source&gt;java:jboss/datasources/MySQLEmployeeManagementDS&lt;/jta-data-source&gt; &lt;/persistence-unit&gt; </code></pre> <p></p> <p><strong>standalone.xml</strong></p> <pre><code>&lt;datasource jndi-name="java:jboss/datasources/MySQLEmployeeManagementDS" pool-name="MySQLEmployeeManagementDS" enabled="true" jta="true" use-java-context="true" use-ccm="true"&gt; &lt;connection-url&gt; jdbc:mysql://localhost:3306/employee_management &lt;/connection-url&gt; &lt;driver&gt; mysql-connector-java-5.1.25.jar &lt;/driver&gt; &lt;pool&gt; &lt;min-pool-size&gt; 10 &lt;/min-pool-size&gt; &lt;max-pool-size&gt; 100 &lt;/max-pool-size&gt; &lt;prefill&gt; true &lt;/prefill&gt; &lt;use-strict-min&gt; false &lt;/use-strict-min&gt; &lt;flush-strategy&gt; FailingConnectionOnly &lt;/flush-strategy&gt; &lt;/pool&gt; &lt;security&gt; &lt;user-name&gt; root &lt;/user-name&gt; &lt;password&gt; test &lt;/password&gt; &lt;/security&gt; &lt;/datasource&gt; &lt;drivers&gt; &lt;driver name="mysql-connector-java-5.1.25.jar" module="com.mysql"&gt; &lt;xa-datasource-class&gt; com.mysql.jdbc.jdbc2.optional.MysqlXADataSource &lt;/xa-datasource-class&gt; &lt;/driver&gt; &lt;/drivers&gt; </code></pre> <p><strong>Table</strong></p> <pre><code>CREATE TABLE `employee` ( `employee_number` int(11) NOT NULL AUTO_INCREMENT, `firstname` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL, `inital` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL, `lastname` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL, `age` int(11) DEFAULT NULL, `birthday` datetime DEFAULT NULL, `street` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL, `house_number` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL, `postalcode` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL, `city` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL, `department` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL, `sallery_group` varchar(128) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`employee_number`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload