简体   繁体   中英

No validator could be found for type: java.lang.Object

I'm using the javax.validation API in my project and have the titular problem on some of developer machines and on the test server. On other developer machines this bug can't be reproduced despite the fact that we are using the same Glassfish version, IDE and library stack handled by maven.

While debugging this problem we suspected something that may be the cause of the problem (we can't debug inside compiled classes, therefore 'suspected') - we have an interface for our persistable entities:

public interface PersistableEntity<T> {

    public T getId();

    public void setId(T id);
}

Implementation classes are split by 'id' field type: String or BigDecimal . The @Pattern constraint is attached to the getter of the id field in the PersistableEntity<String> implementation.

If this is the cause, is it possible to maintain validation using javax.validation and use generics in the same time?

Update We decided to move from JSR303 to custom configurable validation mechanism. If someone with titular problem finds it's solution, please let me know which one it is, and I'll mark it as an answer (any other solutions to this dilema from experienced SO users are welcome).

I'm usually using the Persistable interface of Spring Data, but the idea seems to be the same as your custom one.

Let's say my user table has two fields: user_id (PK) and email (NN). The JAVA User entity will then have a getUserId() getter and a getEmail() one.

When implementing the Persistable interface, a getId() getter must be overridden, while the table user doesn't contain an id field. You can explicitly tell JAVA that the corresponding table doesn't contain such a field by using the @Transient annotation:

@Entity
@Table(name = "user")
public class User implements Persistable<Integer> {

    // Persistable implementation

    @Override
    @Transient // <-- means "not a database field"
    public Integer getId() {
        return getUserId();
    }

    // user_id

    @Column(name = "user_id", precision = 10)
    @GeneratedValue
    @Id
    public Integer getUserId() {
        return userId;
    }

    // email

    @NotNull
    @Column(name = "email", nullable = false, unique = true)
    public String getEmail() {
        return email;
    }

}

Maybe this could solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM