0

Here's my use case

My JPA Repo interface

@Repository
public interface MyJpaRepository extends JpaRepository<MyEntity, Integer> {

//Example query that illustrates working with multiple tables only. Ignore the details of join conditions
    @Query(value="SELECT a.p, a.q,a.r , b.s, b.t, c.u,c.v FROM a, b, c
WHERE a.p=?1,b.s=?2,c.u=5"+ 
            "ORDER BY b.t",nativeQuery = true)
    List<MyEntity> findByPAndS(String p, Integer s);

}

In this scenario, how should my Entity look like? Here's my draft

@Entity
public class MyEntity {

    @Column
    private Integer p;

    @Column
    private Integer q;

    @Column
    private String r;

    @Column
    private String s;

    @Column
    private String t;

    @Column
    private String u;

    @Column
    private Double v;

    public MyEntity(){
    }
}

Are there any issues with my Entity declaration? How does JPA/Spring Data infer which table a specific column is associated with? If I need to define that explicitly, how can I do that?

1
  • Entities are mapped to tables. If you want to map individual values from various tables to a Java object then see stackoverflow.com/questions/16420697/… or create a DB view comprising the necessary data and map an entity to that. Commented Dec 14, 2016 at 20:28

2 Answers 2

0

As said by Alan Hay, one @Entity class ist usually mapped to one table in your database. Without a "name" parameter the table name is infered by the class name (class MyFoo -> Table "MyFoo"). If you wish to refer another table you can explicitly set it like so:

@Entity(name = "MyTableName")
class MyFoo {}

Concerning your class members: if the member name matches the columns name you don't necessarily need the @Column Annotation.

@Entity
class MyFoo {
    private String bar;
}

Will map to Table "MyFoo" with column "bar", until you explicitely use another name:

@Column(name = "Mycolumn")
private String bar;

As you have 3 Tables one usually would create 3 @Entity classes and 3 Repositories to access them and the domain objects reference each other (see @OneToOne, @OneToMany, and so on,..)

If you definitely want to stick with a single Entity for multiple tables you might check this SO answer, using @SecondaryTable: is-possible-map-a-single-entity-with-multiple-tables-using-jpa

Sign up to request clarification or add additional context in comments.

Comments

0
@Entity
@Table
public class Employee {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)  

   private int eid;
   private String ename;
   private double salary;
   private String deg;

   public Employee(int eid, String ename, double salary, String deg) {
      super( );
      this.eid = eid;
      this.ename = ename;
      this.salary = salary;
      this.deg = deg;
   }

   public Employee( ) {
      super();
   }

   public int getEid( ) {
      return eid;
   }

   public void setEid(int eid) {
      this.eid = eid;
   }

   public String getEname( ) {
      return ename;
   }

   public void setEname(String ename) {
      this.ename = ename;
   }

   public double getSalary( ) {
      return salary;
   }

   public void setSalary(double salary) {
      this.salary = salary;
   }

   public String getDeg( ) {
      return deg;
   }

   public void setDeg(String deg) {
      this.deg = deg;
   }
}

I think you need this link.enter link description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.