Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data query from method doesn't recognize column

I am using Spring Data, I have created my entities wrapped inside of an "AbstractEntity" which all objects extend to get basic columns

AbstractEntity:

@MappedSuperclass
public abstract class AbstractEntity implements Serializable{

    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false)
    private Date CreatedDate;

    @PrePersist
    protected void onCreate() {
        UpdatedDate = CreatedDate = new Date();
    }

...

And my Object/Entity

@Entity
public class Trade extends AbstractEntity {

When I attempt to use my repository to create a method to findByCreatedDateAfter(Date date)

I get an exception that the column can't be found...?

public interface TradeRepository extends CrudRepository<Trade, Long>{

    public List<Trade> findByCreatedDateAfter(Date date);
}

It compiles (if I use certain capitolization) but than attempting to map the query, I get:

Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute  with the given name [createdDate] on this ManagedType [streaming.data.AbstractEntity]

I also would like to return the sum(amount) of one of the amount column for this period.

like image 551
jordan.baucke Avatar asked Jan 27 '14 16:01

jordan.baucke


1 Answers

The query derivation mechanism requires standard Java property naming conventions to be applied. There's no property createdDate as it's called CreatedDate in your class.

To customize the mapping to a certain column, use the @Column annotation (hence the name).

like image 170
Oliver Drotbohm Avatar answered Oct 31 '22 15:10

Oliver Drotbohm