Reputation: 69
i was trying to create method (Which is a common method so that i can pass any Table class object to it ) like below which will take List
of TableRecord
as a argument and get table name and column names as well so i am doing below code .
public void getColumns(List<TableRecord> records){
for(TableRecord record : records){
Table table = record.getTable();
Field[] fields = table.fields();
for(Field field : fields){
field.getName();
record.getValue(field);
}
}
}
but my question here how to create a object of TableRecord
class? so that i can call the above method and pass List
of TableRecord
object.
I was trying a generic method which will get TableRecord object and return the database table name and all the column's that table have . I have to use TableRecord
or any other object for this and how to create that object?
I can not do something like this
TableRecord record = new TableRecord()
because it already a interface then how to create object? Or i have to pass another class to method?
EDIT :-
If you will see my code you can check method taking List
of TableRecord
as a argument so that this List
can take any DB table JOOQ class like LoginRecord.java
,EmpRecord.java
etc. So method is not generic and its broad, developer can pass any type of Jooq created table class object by this. Now i have 'List<TableRecord>
' object now i have to get columnName or fieldName and their value i mean data those fields contain, so that i can set data to another bean class which will interact with UI.
Upvotes: 3
Views: 2455
Reputation: 2971
At firt you need to know that TableRecord
is an interface so it is obvious you cannot crate it on your code. What you need to do is find class which implements this interface and fits your requirements. Since jooq hierarchy is very complicated I try to show you how to find best option.
public interface TableRecord<R extends TableRecord<R>> extends Record
then this interface is in:
public interface UpdatableRecord<R extends UpdatableRecord<R>> extends TableRecord<R>
And jooq have it's implementations:
public class TableRecordImpl<R extends TableRecord<R>> extends AbstractRecord implements TableRecord<R>
public class AddressRecord extends org.jooq.impl.UpdatableRecordImpl<AddressRecord> implements org.jooq.Record8<Integer, String, String, String, Float, Float, Integer, String>
public class UpdatableRecordImpl<R extends UpdatableRecord<R>> extends TableRecordImpl<R> implements UpdatableRecord<R>
Time for practise
This is just a base scaffolding of Jooq but one thing is important. As far as you are using jooq you more likely have generated tables, and tables record for each database table in your code. This can look like:
public class AddressRecord extends org.jooq.impl.UpdatableRecordImpl<AddressRecord> implements org.jooq.Record8<Integer, String, String, String, Float, Float, Integer, String>
So your all records extends org.jooq.impl.UpdatableRecordImpl<AddressRecord>
which in fact implements TableRecord<R>
Method parameter
As far as you use interface as parameter you can use polimorphic calls to every object which implements TableRecord<R>
. In this case you can put here all of your records.
List<TableRecord> records = new List<TableRecord>();
records.add(new AddressRecord());
records.add(new CityRecord());
records.add(new UserRecord());
Is this code generic?
If you want this code work for any kind of TableRecord
this code is suitable for you. But this code is not strictly generic
because you don't use generic types. Nevertheless I would keep this code in that manner.
Upvotes: 2
Reputation: 221380
From your comments I take that you are using jOOQ's code generator to generate tables. Hence, you do not need to try to instanciate TableRecord
, but use the generated references instead. In order to create new records, just use:
MyTableRecord record = DSL.using(configuration).newRecord(MY_TABLE);
This is DSLContext.newRecord(Table)
as explained in the chapters about using jOOQ for CRUD
Upvotes: 2