0

I need to create a table using Jackcess library which consists of foreign key reference column and primary key column with auto increment. Also, how can I specify default values for all the columns like Date/Timestamps?

In the Jackcess cookbook, I found one example to create table but it is not covering the above cases.

How can I implement the above?

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Prince
  • 33
  • 1
  • 8
  • Welcome to StackOverflow. Thanks and other chit-chat (read [help→tour](http://stackoverflow.com/tour)), should never be included in a question. – Anthon Mar 09 '15 at 08:10
  • 1
    you should be able to do everything except the foreign key reference. at this point in time, jackcess does not support creating foreign key indexes. – jtahlborn Mar 23 '15 at 12:56
  • 1
    as of the 2.1.5 release of jackcess, you can create foreign key references. – jtahlborn Oct 04 '16 at 03:39

1 Answers1

2

Creating Relationships

Jackcess 2.1.5 added the ability to create Relationships (and hence Foreign Key Constraints) using RelationshipBuilder, e.g.

// example in the JavaDoc for RelationshipBuilder:
//
Relationship rel = new RelationshipBuilder("FromTable", "ToTable")
     .addColumns("ID", "FK_ID")
     .setReferentialIntegrity()
     .setCascadeDeletes()
     .toRelationship(db);

Other Items

  • Creating a table with an AutoNumber field:

As shown in the cookbook, that is done using ColumnBuilder#setAutonumber(true).

  • Setting a default value for a field:

That can be done by creating a new Property named "DefaultValue" for the column:

Table tbl = db.getTable("Donations");
Column col = tbl.getColumn("DonationDate");
PropertyMap pm = col.getProperties();
pm.put("DefaultValue", "Date()");
pm.save();

Note, however, that while this default value will be used by ACE/Jet and UCanAccess, Jackcess itself does not currently respect the "DefaultValue" property when it adds new rows to a table.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • 1
    Update to this answer, as of 2.2.0 release, jackcess supports optional evaluation of expressions (e.g. default values). the functionality is still in beta, so it defaults to disabled. you can enable it with a system property or by setting a property on the database instance. – jtahlborn Oct 04 '18 at 17:30