1

I have the following bean

@Entity
public class A {
   @id
   @generatedvalue
   int id;
   @Column(name="FIELD1")
   private string field1;

   @Autowired
   private MyType field2;

   //getters and setters
}

I also have a DAO class to access this bean. The application has a database with a table A and 2 fields, field1 and field2. The value that is stored in field2 in the database should be an id of field2, I can ask this via

field2.getId();

How should I annotate this in JPA?

Mogsdad
  • 42,835
  • 20
  • 145
  • 262
user2321611
  • 949
  • 1
  • 6
  • 17

1 Answers1

0

JPA shouldn't be using dependency injection annotations like @Autowired. Instead, it seems that what you want is a many-to-one foreign-key relationship (or perhaps just a one-to-one, if field2 can only be associated with one A). Annotate it like this:

@ManyToOne
private MyType field2;
chrylis -cautiouslyoptimistic-
  • 72,004
  • 20
  • 117
  • 147