How to insert a record with DataManager?

Hi,
I’m trying to insert a new Datarecord with code.

Snippet:
WorkRole rec3 = new WorkRole();
rec3.setName(“test”);
rec3.setDescription(“Dest”);
rec3.setState(StateEnum.active);

    CommitContext commitContext = new CommitContext();
    commitContext.getCommitInstances().add(rec3);
    commitContext.getViews().put(rec3, workRolesDs.getView());
    dataManager.commit(commitContext);

The Entity WorkRole is derived from BaseIntegerIdEntity. The above Code allways claims:

SQLException: Cannot insert the value NULL into column ‘ID’, table ‘salary.dbo.SALARY_WORK_ROLE’; column does not allow nulls. INSERT fails.

Since ID is the PK Field I would expect that the field is handled by the framework? Btw. the underlying DB is MSSQL 2014.

thanx
Josef

1 Like

Hi Josef,

You are right, ID creation is handled by the platform, but on the client tier. So you should always use Metadata.create() methods to create entity instances. Obtain a reference to Metadata either by injection or through AppBeans.get(), and use it:

WorkRole rec3 = metadata.create(WorkRole.class);
rec3.setName("test");
rec3.setDescription("Dest");
rec3.setState(StateEnum.active);
dataManager.commit(rec3);

Long and Integer identifiers are obtained from the database in batches and cached on the middleware and client tiers, so you should not worry about extra roundtrips.

However, I would recommend using UUID identifiers in general, and other types of identifiers only for some particular entities which can affect database size or performance.

Hi Konstatin,

thanx this worked out.
Btw. is it ok to refresh the Screen afterwards over the filter component e.g.

@Inject
private Filter filter;

filter.apply(false);

Better use CollectionDatasource.refresh(), because it works regardless of Filter presence:

@Inject
private CollectionDatasource<Customer, UUID> customersDs;
...
customersDs.refresh();