#Identifier error when UPDATE request

62 messages · Page 1 of 1 (latest)

warped laurel
#

I have a profile update request in my ProfileController, but when I try to send this update request in my MineCraft plugin, I get this error:
org.hibernate.id.IdentifierGenerationException: Identifier of entity 'net.lydecube.galapy.model.profile.Profile' must be manually assigned before calling 'persist()'

ProfileController#update:

    @PutMapping
    public ResponseEntity<?> updateProfile(@RequestBody UpdateProfileDto updateProfileDto) {
        // We create updating profile by using modelMapper, and we check if it exists
        val profile = modelMapper.map(updateProfileDto, Profile.class);
        if (profileService.existsById(updateProfileDto.getId())) {
            // We check and update ipHistory
            if (!ipHistoryService.exists(updateProfileDto.getId(), updateProfileDto.getLastIp())) {
                IpHistory ipHistory = new IpHistory();
                ipHistory.setProfile(profile);
                ipHistory.setId(updateProfileDto.getId());
                ipHistory.setAddress(updateProfileDto.getLastIp());
                profile.setLastIp(ipHistory);
                IpHistory oldIpHistory = ipHistoryService.findByProfileId(updateProfileDto.getId()).get();
                oldIpHistory.setLastUsed(new Timestamp(System.currentTimeMillis()));
                ipHistoryService.save(ipHistory);
                ipHistoryService.save(oldIpHistory);
            }
            // We save updating profile, and we return it
            profileService.update(profile);
            return ResponseEntity.ok(profileMapper.mapProfile(profile));
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Profile was not found.");
        }
    }```
quartz galeBOT
#

This post has been reserved for your question.

Hey @warped laurel! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

warped laurel
#

MinecraftPlugin#updateProfile:

    public Profile update(Profile profile) {
        // We check if it exists
        if (findById(profile.getUuid()) != null) {
            try {
                // We update this profile on Database side, and we assign it
                profile = http.request("/profiles", Method.PUT, Profile.class, profile);
                // We update this profile on Redis side
                redis.getBucket("profiles:" + profile.getUuid().toString() + ":" + profile.getName()).setAsync(profile);
            } catch (RestException exception) {
                // We check if it saves otherwise we return null
                if (exception instanceof EntitySaveException) {
                    return null;
                }
                throw new RestException("Previous:", exception);
            }
        }
        return profile;
    }```
#

Calling update method in ProfileListener:

        } catch (EntityNotFoundException exception) {
            profile = new Profile();
            try {
                profile.setUuid(event.getConnection().getUniqueId());
                profile.setName(event.getConnection().getName());
                profile.setLastIp(event.getConnection().getAddress().getAddress().getHostAddress());
                profile = profileRepository.create(profile);
            } catch (EntityCreateException createException) {
                event.setCancelled(true);
            }
            Permission bypass = new Permission();
            bypass.setName("_admin.bypass");
            profile.addPermission(bypass);
            ProxyServer.getInstance().broadcast(new TextComponent("§4DEBUG PROFILE: " + profile));
            RestProvider.getProfiles().update(profile);
            RestProvider.getProperties().create(new Property(profile.getUuid(), "test", PropertyStatus.YES));
        }```
tulip violet
#

Can you show your current Profile class?

#

Is it's @Id annotated with @GeneratedValue?

warped laurel
#
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "profiles")
public class Profile {

    @Id
    @Column(name = "id", nullable = false, length = 36)
    private String id;

    @Column(name = "name", nullable = false, length = 16)
    private String name;

    @Column(name = "lyds", nullable = false)
    private Integer lyds = 0;

    @Column(name = "updated_at")
    private Timestamp updatedAt;

    @Column(name = "lang", nullable = false)
    private Lang lang = Lang.FR;

    @Column(name = "last_server")
    private String lastServer;

    @OneToOne(mappedBy = "profile", cascade = CascadeType.ALL, orphanRemoval = true)
    private IpHistory lastIp;

    @Column(name = "hosts_credits", nullable = false)
    private Integer hostsCredits = 0;

    @Column(name = "created_at", nullable = false)
    private Timestamp createdAt = new Timestamp(System.currentTimeMillis());

    @Column(name = "joined_at", nullable = false)
    private Timestamp joinedAt = new Timestamp(System.currentTimeMillis());

    @OneToOne(mappedBy = "profile", cascade = CascadeType.ALL, orphanRemoval = true)
    private Nickname nickname;

}```
warped laurel
quartz galeBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

tulip violet
tulip violet
#

Then first get the profile from the DB and then update it

#

instead of creating a new profile object by yourself

warped laurel
tulip violet
#

What exactly do you want to update about the profile?

#

Can you show UpdateProfileDto?

#

Does it contain the ID?

#

Do you want to completely overwrite the profile with the one from the DTO?

warped laurel
# tulip violet Can you show `UpdateProfileDto`?

UpdateProfileDto:

@Value
public class UpdateProfileDto implements Serializable {

    @Serial
    private static final long serialVersionUID = -7903671301817465468L;

    String id;

    String name;

    Integer coins;

    @JsonProperty("last_ip")
    String lastIp;

    Lang lang;

    String lastServer;

    Timestamp createdAt;

    Timestamp joinedAt;

    Timestamp updatedAt;

    Set<ProfileRoleDto> roles;

    Set<PermissionProfileDto> permissions;

}```
#

It contains the id

tulip violet
#

When running modelMapper.map(updateProfileDto, Profile.class);, is the id set in the returned profile?

warped laurel
#

I think otherwise this line would be in error: if (profileService.existsById(updateProfileDto.getId())) {

tulip violet
#

ah right I didn't see that

#

Can you show the full stack trace? When exactly does the issue happen?

#

You could try first detaching the existing profile and then updating with the new one

warped laurel
# tulip violet Can you show the full stack trace? When exactly does the issue happen?

I have this listener in my MinecraftPlugin:

    @EventHandler
    public void onLogin(LoginEvent event) {
        Profile profile;
        try {
            profile = profileRepository.findById(event.getConnection().getUniqueId());
            boolean save = false;
            if (!profile.getName().equals(event.getConnection().getName())) {
                profile.setName(event.getConnection().getName());
                save = true;
            }
            String ip = event.getConnection().getAddress().getAddress().getHostAddress();
            ProxyServer.getInstance().broadcast(new TextComponent("§4DEBUG PROFILE join: " + profile));
            if (!profile.getLastIp().equals(ip)) {
                profile.setLastIp(ip);
                save = true;
            }
            if (save) {
                profileRepository.update(profile);
            }
        } catch (EntityNotFoundException exception) {
            profile = new Profile();
            try {
                profile.setUuid(event.getConnection().getUniqueId());
                profile.setName(event.getConnection().getName());
                profile.setLastIp(event.getConnection().getAddress().getAddress().getHostAddress());
                profile = profileRepository.create(profile);
            } catch (EntityCreateException createException) {
                event.setCancelled(true);
            }
            Permission bypass = new Permission();
            bypass.setName("_admin.bypass");
            profile.addPermission(bypass);
            ProxyServer.getInstance().broadcast(new TextComponent("§4DEBUG PROFILE: " + profile));
            RestProvider.getProfiles().update(profile);
            RestProvider.getProperties().create(new Property(profile.getUuid(), "test", PropertyStatus.YES));
        }
        permissions.put(event.getConnection().getUniqueId(), profile);
    }```
#

The error comes from RestProvider.getProfiles().update(profile);

#

The update method of my MinecraftPlugin:

    public Profile update(Profile profile) {
        // We check if it exists
        if (findById(profile.getUuid()) != null) {
            try {
                // We update this profile on Database side, and we assign it
                profile = http.request("/profiles", Method.PUT, Profile.class, profile);
                // We update this profile on Redis side
                redis.getBucket("profiles:" + profile.getUuid().toString() + ":" + profile.getName()).setAsync(profile);
            } catch (RestException exception) {
                // We check if it saves otherwise we return null
                if (exception instanceof EntitySaveException) {
                    return null;
                }
                throw new RestException("Previous:", exception);
            }
        }
        return profile;
    }```
tulip violet
#

and what's that?

                ipHistoryService.save(ipHistory);
                ipHistoryService.save(oldIpHistory);
warped laurel
tulip violet
#

You have a 1:1 mapping between profile and IPHistory

#

that means you can only have one IPHistory per profile

warped laurel
tulip violet
#

I wanted to know where the error comes from inside updateProfile

tulip violet
warped laurel
# tulip violet I wanted to know where the error comes from inside `updateProfile`

Caused by: net.lydecube.lccore.common.rest.exception.RestException at net.lydecube.lccore.common.rest.http.HttpClient.request(HttpClient.java:158) ~[?:?] at net.lydecube.lccore.common.rest.repository.ProfileRepository.update(ProfileRepository.java:105)

Line 105: profile = http.request("/profiles", Method.PUT, Profile.class, profile);

tulip violet
#

What about sharing the full stack trace?

tulip violet
#

Because it seems like your entity is severely broken

warped laurel
tulip violet
#

@OneToMany on the Profile side

#

and @ManyToOne on the IPHistory side

#

and make sure to use a List<IPHistory> on the Profile side

warped laurel
warped laurel
tulip violet
#

And then I think it's best to change your update logic to just query the profile and then update all things from the DTO

#

Alternatively you can try other things but idk whether these work

tulip violet
tulip violet
warped laurel
warped laurel
tulip violet
#

Like I would probably make a service method that takes the DTO and updates everything

warped laurel
#

Oh okay I understand, I'll be quick to try this and I'll let you know if it works, thanks anyway

quartz galeBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

warped laurel
#

Profile:

public class Profile {

    @Id
    @Column(name = "id", nullable = false, length = 36)
    private String id;

    @Column(name = "name", nullable = false, length = 16)
    private String name;

    @Column(name = "lyds", nullable = false)
    private Integer lyds = 0;

    @Column(name = "updated_at")
    private Timestamp updatedAt;

    @Column(name = "lang", nullable = false)
    private Lang lang = Lang.FR;

    @Column(name = "last_server")
    private String lastServer;

    @OneToMany(mappedBy = "profile", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<IpHistory> lastIp;

    @Column(name = "hosts_credits", nullable = false)
    private Integer hostsCredits = 0;

    @Column(name = "created_at", nullable = false)
    private Timestamp createdAt = new Timestamp(System.currentTimeMillis());

    @Column(name = "joined_at", nullable = false)
    private Timestamp joinedAt = new Timestamp(System.currentTimeMillis());

    @OneToOne(mappedBy = "profile", cascade = CascadeType.ALL, orphanRemoval = true)
    private Nickname nickname;

    @OneToMany(mappedBy = "profile", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<ProfileRole> roles = new LinkedHashSet<>();

    @OneToMany(mappedBy = "profile", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<PermissionProfile> permissions = new LinkedHashSet<>();

    @OneToMany(mappedBy = "target", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<Punishment> punishments = new LinkedHashSet<>();

    @OneToMany(mappedBy = "target", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<Report> reports = new LinkedHashSet<>();

    @OneToMany(mappedBy = "profile", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<Property> properties = new LinkedHashSet<>();

}```
tulip violet
#

Can you show the update code?

warped laurel