Awesome. Not sure if I should make a different post since it's kinda a
different issue. But i'll post it here for now.
So I've been trying load custom POJOs per-say back into records to store
them, but it's not working as expected for abstract classes for some reason.
Here is my abstract class :
public abstract class MyBook {
public int id;
protected String name;
public int getId() {
return id;
}
public void setId(int newId) {
this.id = newId;
}
public String getName() {
return name;
}
public void setName(String newName) {
this.name = newName;
}
}
here is my BookClass that extends the abstract class posted above:
public class BookClass extends MyBook {
// load, save here
}
here is the loading values from the DB (which works fine), it returns the
DB value for ID, Name to the variable.
public static void loadFromDB() {
// init connection and DSL context
DataSource dataSource = connection();
DSLContext create = DSL.using(dataSource, SQLDialect.MYSQL_8_0);
// check to see what current values for MyBook class are before mapping
values from the db
print("un mapped id: " + BookClass.getId() + " un mapped name: " +
BookClass.getName());
// The various "into()" methods allow for fetching records into your
custom POJOs:
// MyBook myBook = create.select(BOOKS.ID,
BOOKS.NAME).from(BOOKS).fetchAny().into(BookClass.class);
MyBook myBook =
create.select().from(BOOKS).fetchAny().into(BookClass.class);
print("mapped id: " + BookClass.getId() + " mapped name: " +
BookClass.getName());
}
But, when I try to save/update It's not performing any queries. here is the
method:
public void save() {
// init connection and DSL context
DataSource dataSource = connection();
DSLContext create = DSL.using(dataSource, SQLDialect.MYSQL_8_0);
// create a new POJO instance
MyBook myBook = new BookClass();
myBook.id = 1;
myBook.name = "Tom";
// Loading a JOOQ-generated BookRecord from POJO
BooksRecord book = create.newRecord(BOOKS, myBook);
// we just want to update the already existing name
create.executeUpdate(book);
// check to see if changes were successful.
print("id = "+ BookClass.getId() + " name = " + BookClass.getName());
print(create.executeUpdate(book));
}
It's not saving the newly assigned values to the DB. But if use this class
instead without extending any abstract class:
public class TestBook {
public int id;
public String name;
}
It'll perform the update.
Been spending a few hours trying to figure this out, but i'm lost~ Any help
would be much appreciated!
Also on a side note, does JOOQ have a public community discord? If not any
plans for one?
Thanks!
On Thursday, May 3, 2018 at 3:28:27 AM UTC-4, Lukas Eder wrote:
>
>
>
> 2018-05-03 4:59 GMT+02:00 xragons <[email protected] <javascript:>>:
>
>> Does this look right and efficient or Is there a better way to do this?
>>
>
> Yes, that looks about right
>
>
>> My 2nd concern is, about try/catch and closing connections. As you can
>> see in the first example, I'm using try catch blocks AND i'm also manually
>> closing all the open statements. In the 2nd example, if I don't include a
>> try/catch would it still close the opened connections?
>> I think I was looking at one of your tutorials on github and saw
>> something similar. Furthermore, is a try/catch/closing statements needed
>> for other queries as well? (update, delete, etc) or does JOOQ handle those
>> too?
>>
>
> jOOQ manages resources for you. For a list of differences between jOOQ and
> JDBC, see this page here:
> https://www.jooq.org/doc/latest/manual/sql-execution/comparison-with-jdbc
>
> I think I'm going to improve that page with actual examples:
> https://github.com/jOOQ/jOOQ/issues/6102
>
> Cheers,
> Lukas
>
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.