0

I have a MySQL table:

mysql> show create table items\G
*************************** 1. row ***************************
       Table: items
Create Table: CREATE TABLE `items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
1 row in set (0.02 sec)

I create new rows from a Java program via an Entity class:

@Entity
@Table(name = "items", schema = "office_db")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Items.findAll", query = "SELECT i FROM Items i"),
    @NamedQuery(name = "Items.findById", query = "SELECT i FROM Items i WHERE i.id = :id"),
    @NamedQuery(name = "Items.findByName", query = "SELECT i FROM Items i WHERE i.name = :name"),
    @NamedQuery(name = "Items.findByCreated", query = "SELECT i FROM Items i WHERE i.created = :created")
})
public class Items implements Serializable {

    @Column(name = "name",length = 128)
    private String name;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "created")
    @Temporal(TemporalType.TIMESTAMP)
    private Date created;
    @OneToMany(mappedBy = "itemId")
    private Collection<Documents> documentsCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "items")
    private Collection<ItemAttributes> itemAttributesCollection;
    (more stuff ...)

I only set the NAME column, and as expected, the ID and CREATED are set by default:

mysql> select * from items;
+----+--------+---------------------+
| id | name   | created             |
+----+--------+---------------------+
|  2 | Case 2 | 2017-10-31 13:47:52 |
|  3 | Case 3 | 2017-10-31 13:48:02 |
+----+--------+---------------------+
2 rows in set (0.00 sec)

However, when I reload the table into Java later in the same session:

public List<Items>findItems(){
    TypedQuery<Items> query=
            em.createNamedQuery("Items.findAll",Items.class);

    return query.getResultList();
}

the ID column is loaded correctly, but the CREATED column comes up as blank. CREATED shows up correctly if I relaunch the application (this runs on a glassfish server). My guess is that the reason for this difference is the @GeneratedValue annotation on ID, but I can't apply it on the CREATED field, it seems, or at least not naively. What is the correct way to make the generated timestamp load?

j4nd3r53n
  • 680
  • 2
  • 11
  • 26
  • when you persisted an object, what SQL INSERT statement was invoked? Then when you execute the query where does it get the objects from ? from the datastore? or from the L1/L2 caches? look at the log –  Oct 31 '17 at 14:05
  • I don't see it in the log (the server.log for this domain) - all I see there is the INFO log output I have in the code to trace what is going on. I will have to research the Glasshfish settings to see if I can get to see the SQL and that. Any pointers in that direction will be appreciated. – j4nd3r53n Oct 31 '17 at 14:18
  • Its whatever JPA provider you chose, so look in their documentation for where their log is located –  Oct 31 '17 at 14:24
  • Ah well, I seem to have broken logging altogether in Glassfish now. I just changed one of the logger settings to FINE, and now it doesn't work. Anyway, I think it is safe to conclude from the symptoms, that the INSERT only sets the NAME col, and that the reload seems to simply use the cache. Is there a way to force it to load the select from the table? – j4nd3r53n Oct 31 '17 at 16:02
  • Got logging back - in the interest of others that experience the same problem: find logging.properties in the domain directory; in my case, the property "com.sun.enterprise.server.logging.GFFileHandler.level" should be set to "ALL" (or at least not "OFF") – j4nd3r53n Oct 31 '17 at 16:29
  • So @DN1, you asked for the SQL - when I create new rows, I get something like: "INSERT INTO office_db.items (created, name) VALUES (?, ?) bind => [null, bing]", followed by "SELECT LAST_INSERT_ID()". For the reloading of the list of rows, I see: "SELECT id, created, name FROM office_db.items", which I assume comes from what is in the cache. – j4nd3r53n Oct 31 '17 at 16:34

1 Answers1

0

The answer to my conundrum, it appears, is to call em.refresh(), according to this: Invalidating JPA EntityManager session - this is for Hibernate, but it seems to be the same for EclipseLink. I run in to an exception, in my case, but I will post that in another question.

j4nd3r53n
  • 680
  • 2
  • 11
  • 26