Default primary key setter and getter generated by Service Builder - liferay

I'm working with Liferay. When create an entity, we define one column as the primary key. Then I look at the entity model, I see some methods setPrimaryKey, getPrimaryKey as well as the setter and getter of the column that we defined as the primary key before. What is the difference of these methods? Do we need to use both methods once we add new entity into the table or just one

Assuming that your project is built by many people. Some one designs entity and you only code view page. When you want to get or set primary key of entity but you don't know what column. If don't have setPrimaryKey or getPrimaryKey methods, do you look up in the code to find what column is primary key?

Related

Association without ReferenceVersionField

Is it possible to have a OneToManyAssociationField as entity extension on for example ProductManufacturer without the ReferenceVersionField in my related custom entity?
If this is not possible, is it possible for the reference version field to have a custom name (so not product_manufacturer_version_id) On first sight, this also does not seem possible.
About the error
I am currently getting the following error when trying to search for manufacturers using $criteria->addAssociation('myCustomEntity'):
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'product_manufacturer.myCustomEntity.product_manufacturer_version_id' in 'field list'
About the big picture
The use case is similar to the SeoUrl entity where there is a ‘foreign_key’ field which can have a relation to multiple entity types. My entity has not associations, but the other entities are extended to have an association to my entity. Just like the SeoUrl.
However, the DAL creates a query which uses the ‘product_manufacturer_version_id’ field, which does not exist on my custom entity…
Is it possible to have a OneToManyAssociationField as entity extension on for example ProductManufacturer without the ReferenceVersionField in my related custom entity?
No, you must set a ReferenceVersionField when adding associations to the definition of a versionized entity. This is too deeply rooted in the basic principles of the data abstraction layer to work around.
If this is not possible, is it possible for the reference version field to have a custom name (so not product_manufacturer_version_id) On first sight, this also does not seem possible.
You can change the storage name of the field. That is the name of the corresponding column within your database table. When you instantiate ReferenceVersionField you can use the second, optional argument to provide the storage name:
public function __construct(string $definition, ?string $storageName = null)
The storage name should be provided in snake case. The name of the object property for the field will then be derived from the storage name and converted to camel case. So given you provide my_version_custom_id for the storage name, the object property of the entity will be myVersionCustomId.
Your entity may have multiple associations to different entities, but if those entities are versionized your foreign key constraint has to be a combination of columns for both the foreign primary key as well as the foreign version id.

Can OrmLite specify foreign key to an attribute other than the primary key

I have a table structure where a foreign key from Employee table references something other than the primary key of the Department table. This is for historical reasons, so that's just the way it is.
This works: q.Join<Department>((e, d) => e.DepartmentId == d.DepNo);. Note that Department's primary key is Id.
Now, is there any way I could specify the join relationship (with the column name)? The reason is that I'd like to use AutoQuery's built-in IJoin<Employee,Department> thing, but that doesn't let me specify the columns to use.
Please see docs on Reference Conventions for how to define implicit PK and FK references.
AutoQuery only supports implicit references, if you need more customized behavior you’ll need to create a custom AutoQuery implementation.

JOOQ Code generation could take into account composite unique constraints

Jooq - 3.13
We are using Jooq Codegen to generate using the org.jooq.meta.extensions.ddl.DDLDatabase generation strategy.
When I have a table ex: Employee with a unique key constraint on say employee_number (this column is not a primary key) it generates a very useful method fetchOneByEmployeeNumber however I cannot say the same about composite keys.
If there is a composite unique key on (department_id, employee_number) then I'd like a method findOneByDepartmentIdEmployeeNumber(Long departmentId, Long employeeNumber). Is this possible? (Obviously I can roll my own method for this purpose but we have quite a few of these constraints and auto generated methods will be very helpful)
This isn't being done right now for DAOs in jOOQ's code generator. I've created a feature request for this: https://github.com/jOOQ/jOOQ/issues/10597
As a workaround, you can extend jOOQ's JavaGenerator::generateDaoClassFooter method to generate your own, see: https://www.jooq.org/doc/latest/manual/code-generation/codegen-custom-code/

Creating associations in EDM model on unique keys

I'm developing a new application which is based on a legacy database. The old legacy database does not use reference integrity and first we try to not change the existing schema. I still want to be be able to use navigation properties in my EF generated POCO classes.
However, the old data model has one big issue regarding child-parent relationships: the column in the child table is referencing the parent table via the uniqe key of the parent, not via the primary key of the parent table. Is it still possible to generate such associations?
I tried to mark the unique key in the parent table as "entity key" but then I still need to provide a mapping for the parent table's primary key which I am not able to because there is no mapping for it available, this primary is just a dummy "counter". If I do not provide a mapping for the primary key, I get
"Error 111: Properties referred by the Principal Role XXX must be exactly identical to the key of the EntityType YYY referred to by the Principal Role in the relationship constraint for Relationship ZZZ. Make sure all the key properties are specified in the Principal Role"
No EF does not support unique keys at all. It is hopefully planned feature of the next major release.

How can I obtain the database schema from an existing ActiveRecord.cs file?

I have been given the source code for an existing project that uses SubSonic ORM. My (limited!) understanding is that SubSonic generates code by reverse-engineering the existing database. Unfortunately I don't have the database that was used for this project.
I do have the ActiveRecord.cs file from the last time it was compiled. How could I work out the database schema so I can reproduce the database?
This sounds like SubSonic 3. Here are a couple places to get you started based on me looking through my ActiveRecord.cs file. You might want to create a small database yourself, run SubSonic on it, and see what gets generated in ActiveRecord.cs.
Inside your ActiveRecord.cs file, you'll find one partial class per table. The partial class will inherit from IActiveRecord and will likely be the name of the table.
Inside the class, you'll find a function called "KeyName()" which will return your primary key column name for the table. SubSonic requires a primary key for tables it processes and generates code for.
Look for a region named " Foreign Keys ". If this table has foreign keys, you'll find a property corresponding to each foreign key, something like "public IQueryable OtherTableNames". So this table should have a column named something like "OtherTableNameID"; check the generated partial class for the foreign key table to be sure.
Immediately below the foreign key region, you'll find properties for the non-foreign key columns of this table. You can somewhat guess at the data types of the columns from the property data types (e.g. string might be a char(x) or a varchar(x)).

Resources