Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jdbc Data Entity with "composite" PK with one field does not work #3269

Open
horiavmuntean opened this issue Dec 18, 2024 · 0 comments
Open

Comments

@horiavmuntean
Copy link

Expected Behavior

The following repository works (not sure if it should because I annotate with @id multiple entity fields)

@JdbcRepository(dialect = Dialect.POSTGRES)
public interface CnvcHome extends CrudRepository<CnvModel, CnvcPK> {        
}
@Introspected(accessKind = AccessKind.FIELD) 
@MappedEntity(value = "RATES", namingStrategy = UpperCase.class)
public class CnvModel {

	@Id
	public String sym; 
	@Id
	public int dat; 

	public Timestamp uti;

	public CnvModel() {
	}
}
@Introspected(accessKind = AccessKind.FIELD)
public class CnvcPK implements Serializable {

    private static final String name = "currency-conversion record";

    public String sym;
    public int dat;

    public CnvcPK() {
    }

    public CnvcPK(String sym, int dat) {
        this.sym = sym.trim();
        this.dat = dat;
    }

    public boolean equals(Object obj) {
        if (this.getClass().equals(obj.getClass())) {
            CnvcPK that = (CnvcPK) obj;
            return this.sym.trim().equals(that.sym.trim()) && (this.dat == that.dat);
        }
        return false;
    }

    public int hashCode() {
        return (sym + dat).hashCode();
    }

    public String toString() {
        return name + ":[symbol=" + this.sym.trim() + "][date=" + this.dat + "]";
    }

}

However if I try with a repo that has a PK that has only one field like below:

@JdbcRepository(dialect = Dialect.POSTGRES)
public interface SymcHome extends CrudRepository<SymModel, SymcPK> {
}
@Introspected(accessKind = AccessKind.FIELD) 
@MappedEntity(value = "SYMC", namingStrategy = UpperCase.class)
public class SymModel {
	@Id
	public String rky;

	public String nam;

	public SymModel() {
	}
}
@Introspected(accessKind = AccessKind.FIELD)
public class SymcPK implements Serializable {

  private static final String name = "symbol record";

  public String rky;

  public SymcPK() {
  }

  public SymcPK(String rky) {
    this.rky = rky.trim();
  }

  public boolean equals(Object obj) {
    if (this.getClass().equals(obj.getClass())) {
      SymcPK that = (SymcPK) obj;
      return this.rky.trim().equals(that.rky.trim());
    }
    return false;
  }

  public int hashCode() {
    return rky.hashCode();
  }

  public String toString() {
    return name + ":[symbol code=" + this.rky.trim() + "]";
  }

}

mn can't generate what it needs to function

Compilation failed; see the compiler output below.
  error: Unable to implement Repository method: SymcHome.findById(Object id). Parameter [SymcPK id] is not compatible with property [java.lang.String rky] of entity: SymModel

My question is - how can I make the second case work without changing the fields of SymModel and SymPK ?

Thanks

Actual Behaviour

No response

Steps To Reproduce

No response

Environment Information

Windows 10
openjdk version "11.0.17" 2022-10-18 LTS

Example Application

No response

Version

4.4.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant