I have the tables Users, Factors, and the cross reference UserFactors.

When i use the store() method on the cross referenced table, after setting both ids like this:

UserFactorsPOJO userFactors = new UserFactorsPOJO();
userFactors.setUserId(userId);
userFactors.setFactorId(FactorId);
userFactors.setValue(value);
UserFactorsRecord userFactRec = create.newRecord(UserFactors.USER_FACTORS, userFactors);
userFactRec.store();

Jooq attempts to insert a record, obviously since i'm setting the ids to the POJO and storing it. I get a "Unique index or primary key violation" error.

How do i retrieve the row i want by using the two ids, and then update that row?

有帮助吗?

解决方案

If you're really updating a single value for a single ID, you shouldn't use jOOQ's UpdatableRecord for the task to avoid issuing two queries. The simplest solution is this:

create.update(USER_FACTORS)
      .set(USER_FACTORS.VALUE, value)
      .where(USER_FACTORS.FACTOR_ID.eq(factorId))
      .execute();

See the manual's section about the UPDATE statement. If you're planning on doing more sophisticated updates, you can also try using jOOQ's MERGE statement support, although in this example, I don't think you'll need MERGE

其他提示

This is the best i could come up with.. Other suggestions are welcome.

    UserFactorsRecord userFactRec = create.fetchOne(UserFactors.USER_FACTORS, 
            UserFactors.USER_FACTORS.FACTOR_ID.eq(factorId).and(UserFactors.USER_FACTORS.USER_ID.eq(userId)));
    userFactRec.setValue(value);
    userFactRec.store();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top