Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not persist data model's field into database, but can retrieve it

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

like image 382
Suule Avatar asked Aug 27 '18 11:08

Suule


People also ask

What is data persistence in database?

Persistence is "the continuance of an effect after its cause is removed". In the context of storing data in a computer system, this means that the data survives after the process with which it was created has ended. In other words, for a data store to be considered persistent, it must write to non-volatile storage.

How do you persist data?

Enabling data persistence on data variablesSelect a data variable from your list. If the list is empty, create a data variable that you want to persist. For the Persist data to value, set to User or Application. Tip: Only persist data for fields that are necessary for your business application.

What is data persistence with example?

Take, for example, writing code to write data to external files for future use. The code which wrote these files has since stopped running, but the files that were created still exist — this is persistence.


2 Answers

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.

like image 53
Suule Avatar answered Nov 11 '22 12:11

Suule


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

like image 2
fviel Avatar answered Nov 11 '22 11:11

fviel