[ 
https://issues.apache.org/jira/browse/DELTASPIKE-1407?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Syed Shah updated DELTASPIKE-1407:
----------------------------------
    Description: 
I have the following code:

{code:java}
@Repository(forEntity = GuildData.class)
public interface GuildRepository extends EntityRepository<GuildData, Long> {

    /**
     * @param id The ID of the guild.
     * @param prefix What to set he guild's prefix to.
     * @return The number of rows that changed, this should always be 1.
     */
    @Modifying
    @Query("UPDATE GuildData AS g SET g.prefix = ?1 WHERE g.id = ?2")
    int updatePrefix(final String prefix, final long id);
}
{code}

However, when I do the following, the database updates successfully, however 
when the application next does a query for the entity it still returns the old 
value until the application has restarted?

{code:java}
private void setPrefix(long guildId, String prefix) {
    int result = guildRepo.updatePrefix(prefix, guildId);
    logger.debug("Prefix command performed and updated {} prefix in the 
database.", result);
}
{code}

This however, gets the desired behavior:

{code:java}
private void setPrefix(long guildId, String prefix) {
    GuildData data = guildRepo.findBy(guildId);

    if (data == null)
        data = new GuildData(guildId);

    data.setPrefix(prefix);
    guildRepo.save(data);
}
{code}

Following the documentation, shouldn't the first version have worked perfectly 
fine, just with fewer queries?

  was:
I have the following code:

{code:java}
@Repository(forEntity = GuildData.class)
public interface GuildRepository extends EntityRepository<GuildData, Long> {

    /**
     * @param id The ID of the guild.
     * @param prefix What to set he guild's prefix to.
     * @return The number of rows that changed, this should always be 1.
     */
    @Modifying
    @Query("UPDATE GuildData AS g SET g.prefix = ?1 WHERE g.id = ?2")
    int updatePrefix(final String prefix, final long id);
}
{code}

However, when I do the following, the database updates successfully, however 
when the application next does a query for the entity it still returns the old 
value until the application has restarted?

{code:java}
private void setPrefix(long guildId, String prefix) {
    int result = guildRepo.updatePrefix(prefix, guildId);
    logger.debug("Prefix command performed and updated {} prefix in the 
database.", result);
}
{code}

This however, gets the desired behavior:

{code:java}
private void setPrefix(long guildId, String prefix) {
    GuildData data = guildRepo.findBy(guildId);

    if (data == null)
        data = new GuildData(guildId);

    data.setPrefix(prefix);
    guildRepo.save(data);
}
{code}



> DeltaSpike Data @Modifying Updates Database, but Selects Retain Old Value
> -------------------------------------------------------------------------
>
>                 Key: DELTASPIKE-1407
>                 URL: https://issues.apache.org/jira/browse/DELTASPIKE-1407
>             Project: DeltaSpike
>          Issue Type: Bug
>      Security Level: public(Regular issues) 
>          Components: Data-Module
>    Affects Versions: 1.9.3
>            Reporter: Syed Shah
>            Priority: Major
>
> I have the following code:
> {code:java}
> @Repository(forEntity = GuildData.class)
> public interface GuildRepository extends EntityRepository<GuildData, Long> {
>     /**
>      * @param id The ID of the guild.
>      * @param prefix What to set he guild's prefix to.
>      * @return The number of rows that changed, this should always be 1.
>      */
>     @Modifying
>     @Query("UPDATE GuildData AS g SET g.prefix = ?1 WHERE g.id = ?2")
>     int updatePrefix(final String prefix, final long id);
> }
> {code}
> However, when I do the following, the database updates successfully, however 
> when the application next does a query for the entity it still returns the 
> old value until the application has restarted?
> {code:java}
> private void setPrefix(long guildId, String prefix) {
>     int result = guildRepo.updatePrefix(prefix, guildId);
>     logger.debug("Prefix command performed and updated {} prefix in the 
> database.", result);
> }
> {code}
> This however, gets the desired behavior:
> {code:java}
> private void setPrefix(long guildId, String prefix) {
>     GuildData data = guildRepo.findBy(guildId);
>     if (data == null)
>         data = new GuildData(guildId);
>     data.setPrefix(prefix);
>     guildRepo.save(data);
> }
> {code}
> Following the documentation, shouldn't the first version have worked 
> perfectly fine, just with fewer queries?



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to