mars_dev
mars_dev

Reputation: 639

The method getId() is undefined for the type ClassName

I am learning Spring boot and got stuck in a problem. I have created an Expense class as following:

@AllArgsConstructor
@NoArgsConstructor
@Entity
@Data
@Table(name="expense")
public class Expense {
    @Id
    private Long id;
    
    private Instant expensedate;
    
    private String description;
    
    private String location;
    
    @ManyToOne
    private Category category;
    
    @JsonIgnore
    @ManyToOne
    private User user;
}

Now I am trying to create a controller to add/delete the Expense objects. Following method is for creating Expense object:

@PostMapping("/expenses")
ResponseEntity<Expense> createExpense(@Valid @RequestBody Expense expense) throws URISyntaxException{
    Expense result = expenseRepository.save(expense);
    return ResponseEntity.created(new URI("/api/expense" + result.getId())).body(result);
}

But here in this method I am getting following error:

The method getId() is undefined for the type Expanse

Upvotes: 1

Views: 6611

Answers (5)

ramal
ramal

Reputation: 25

update intellij IDEA and restart. it may take little time. and install lombok plugin.it works for me.

here , i faced the issue : https://www.youtube.com/watch?v=uNC8v-BVLRU&list=PLGRDMO4rOGcNLnW1L2vgsExTBg-VPoZHr&index=6&t=146s

enter image description here

Upvotes: 0

jdhall
jdhall

Reputation: 101

If the above methods like:

  1. installing Lombak

  2. restarting Eclipse

  3. Clean/Build the project still show the error, try a forth thing:

  4. I deleted the last character of @Data and used intellisense to put it back. Eclipse refreshed the project and the getId() was gone.

Upvotes: 0

anicetkeric
anicetkeric

Reputation: 636

You can use @Setter @Getter annotations

Upvotes: 0

NehaPatil
NehaPatil

Reputation: 86

Try using "@GeneratedValue" below "@Id".

Upvotes: 0

Guilherme Melo
Guilherme Melo

Reputation: 199

Did You have install lombok plugin in your IDE?

If you are using Eclipse:

Lombok download page

If you are using Intellij find the plugin there.

Upvotes: 5

Related Questions