JPA- Joining two tables in non-entity class
@SqlResultSetMapping
can be placed at any entity class (don't annotate POJOs - it won't work). Mapping to POJO class with @ConstructorResult
was added in version 2.1 of JPA. POJO used with the mapping has to have correct constructor.
All columns corresponding to arguments of the intended constructor must be specified using the columns element of the ConstructorResult annotation in the same order as that of the argument list of the constructor.
Please consult following example with query usage and work out your case accordingly.
@Entity
public class Address {
@Id int id;
String street;
}
@SqlResultSetMapping(name="PersonDTOMapping",
classes = {
@ConstructorResult(targetClass = PersonDTO.class,
columns = {@ColumnResult(name="name"), @ColumnResult(name="street")}
)}
)
@Entity
public class Person {
@Id int id;
String name;
Address address;
}
public class PersonDTO {
String name;
String street;
public PersonDTO(String name, String street) {
this.name = name;
this.street = street;
}
}
// usage
Query query = em.createNativeQuery(
"SELECT p.name AS name, a.street AS street FROM Person p, Address a WHERE p.address_id=a.id",
"PersonDTOMapping");
List<PersonDTO> result = query.getResultList();
Please note that aliases (AS name
and AS street
) has to match the names in @ColumnResult
s.
The example was tested against Ecliselink 2.5.1.
Joining 2 tables with non-key columns in JPA, Hibernate
You need to change your mappedBy
to merchant
Foo
@Entity
@Table(name = "skl_transactions")
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
protected Long id;
@Column(name = "column_a")
private String senderAddress;
-----
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="column_b", referencedColumnName = "column_a", insertable=false, updatable=false)
private Bar merchant;
// getters and setters
}
Bar
@Entity
@Table(name = "merchant_config")
public class Bar implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Basic(optional = false)
protected Long id;
@Column(name = "column_b")
private String merchantAddress;
-----
@OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY)
private List<Foo> transactions = new ArrayList<>();
// getters and setters
}
If above have any issue try changing name
and referencedColumnName
the other way round
in Foo
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="column_a", referencedColumnName = "column_b", insertable=false, updatable=false)
private Bar merchant;
in Bar
@OneToMany(mappedBy = "merchant", fetch = FetchType.LAZY)
private List<Foo> transactions = new ArrayList<>();
Joing two tables in JPA repository
So you are querying native queries, so you need to pass nativeQuery = true to query argument. Also, you need to add @Repository annotation to TransictionRepository interface. That is nothing but your Dao layer.
package com.overflow.overflow.service;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.overflow.overflow.models.Transictions;
@Repository
public interface TransictionRepository extends JpaRepository<Transictions, Long> {
@Query(nativeQuery = true,
value = "SELECT transiction.user_id, transiction.quantity,transiction.instrument_name, transiction.Price,instrument.LTP"
+ "FROM instrument"
+ "INNER JOIN transiction"
+ "ON instrument.instrument=transiction.instrument_name")
public List<Object[]> getTransictionsAndInstruments();
}
Related Topics
How to Use Variables in One Method into Another Method
Java Spring: Best Way to Convert a File to a Multipartfile
Java - How to Write My Arraylist to a File, and Read (Load) That File to the Original Arraylist
Java Socket Api: How to Tell If a Connection Has Been Closed
Java.Lang.Illegalargumentexception: Not a Managed Type: Class
Flush/Clear System.In (Stdin) Before Reading
How to Write a User Input Based Constructor
Launch New Activity from Fragment in Android Studio
Bootstrap.Yml Not Loading in Spring Boot 2
Calculating the Sum of Number and Its Reverse in Java
Jparepository Findall() Returns Empty Result
Parameter 0 of Constructor in Required a Bean of Type 'Java.Lang.String' That Could Not Be Found
How to Find an Object in an Arraylist by Property
Spring Junit: How to Mock Autowired Component in Autowired Component
Spring Data JPA and Parameters That Can Be Null
Use String.Split() With Multiple Delimiters
How to Get the Autoincremented Id When I Insert a Record in a Table Via Jdbctemplate