Skip to content

'findOne()' returns bean with isNew() = true #3754

@PromanSEW

Description

@PromanSEW

Why? Here is code:

@Entity
public class EventProgressFishSoup extends Model {
	@JsonIgnore
	@OneToOne(optional = false, fetch = FetchType.LAZY)
	public User user;
	public int result;
	public int received;
	@DbJsonB
	@Column(nullable = false)
	public JsonNode progress;

	public static void updateResult(int user_id, JsonNode json) {
		EventProgressFishSoup record = DB.find(EventProgressFishSoup.class).where().eq("user_id", user_id).findOne();
		if (record != null) {
			System.out.println(DB.beanState(record).isNew()); // true
			var bean = ((EntityBean) record)._ebean_getIntercept();
			System.out.println("isNew before any changes: " + bean.isNew()); // true
			System.out.println("isReference: " + bean.isReference()); // false
			record.progress = progress;
			System.out.println("isUpdate after: " + bean.isUpdate()); // false
			System.out.println("isNew after: " + bean.isNew()); // true
			save(); // DataIntegrityException: user = NULL, tries INSERT instead of UPDATE
		}
		// ....
	}
}

Ebean 17.1.0, Ebean Agent 17.1.0
Class is enhanced, of course.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions