13

I have a problem when trying to persist a data model class into a database. I have a class like this:

class DataModelClass{
    //some more field etc.

    @Column(name = "number1", nullable = true)
    private Integer number1;

    @Column(name = "number2", nullable = true)
    private Integer number2;

    public DataModelClass(){}

    (...)

    public Integer getNumber2() {
        return number2;
    }

    public void setNumber2( Integer number2 ) {
        this.number2= number2;
    }
}

The second field was added after first one. When to persist object created with this class via:

em.persist(dataModelClass);

A new row in database is created, but only with first field added. The second one is empty. When I am debugging the object dataModelClass has every field set with some integer value. When I am adding a value for number2 through pgAdmin, and then retrieving this row with java code via:

DataModelClass dmc = em.find(DataModelClass.class, 1);

Than dmc.getNumber2() is not empty/null.

Anyone have any ideas what is wrong?

[Edit] Maybe it will help a little more, On data model (DataModelClass) class i got this annotation:

@Entity
@Table(name = "custom_table",
       uniqueConstraints=@UniqueConstraint(name="UK_example_foreign_id", columnNames={"example_foreign_id"})
)
@SequenceGenerator(name = DataModelClass.SEQ_NAME, sequenceName = DataModelClass.SEQ_NAME, allocationSize = 1)

Obviously this field exist in my class

Suule
  • 2,197
  • 4
  • 16
  • 42
  • Can you show more code of DataModelClass? How does the setter of number2 look like? – Simon Martinelli Aug 27 '18 at 11:25
  • Yep, I did add some more of example code – Suule Aug 27 '18 at 11:29
  • enable sql queries logging. – simar Aug 27 '18 at 11:33
  • in sql queries logs i can't see anything interesting – Suule Aug 27 '18 at 12:26
  • Try setting a value into number2. – K.Nicholas Aug 27 '18 at 17:15
  • Well, I thought about this, hoping that while debugging I did't spot something. But even when I set number2 to some specific value just before persisting to db I get empty field in postgres – Suule Aug 27 '18 at 17:36
  • Have you looked at it on the database level? What do you see when you do a "SELECT * FROM datamodelclass"? Is the field null as well? Is it existing? I assume that you schema was not upgraded after you introduced the second field. – Maximilian C. Aug 27 '18 at 17:45
  • @MaximilianC. Yes, when I am selecting/getting particular record from table, the value for datamodelclass.number2 is null. What do you mean by not upgraded schema? – Suule Aug 27 '18 at 19:54
  • My worry was that the particular database table might only have a column "number1" and not the column "number2". From your comment, I take this is not the case. – Maximilian C. Aug 27 '18 at 20:17
  • Could you pls provide some more details? Which JPA implementation are you using? Are there any log outputs from the JPA implementation? Any config files you could share with us? – Maximilian C. Aug 27 '18 at 20:18
  • Well, I was updating the table by hand in pgAdmin for test purposes. Currently I don't have access to my workspace. I will try to post some more information in like 8 hours – Suule Aug 27 '18 at 20:31
  • might you add the generated insert query? – Zeromus Aug 29 '18 at 13:16
  • You need to provide full code of how you write and how you read. If you are writing with em.persist but reading with a query then you are probably executing the code within the same transaction. In that case the database values have not been written and you need to commit the transaction before trying to read the values from the database. – K.Nicholas Aug 31 '18 at 16:45

2 Answers2

2

I would check if my database is updated as my entity class.

fviel
  • 46
  • 3
2

The problem was, that after persist there was another query which was updating the database with null value. So the answer was to change this value in update query. Thanks all.

Suule
  • 2,197
  • 4
  • 16
  • 42