CopyPastor

Detecting plagiarism made easy.

Score: 1.7252313494682312; Reported for: String similarity, Exact paragraph match Open both answers

Possible Plagiarism

Reposted on 2018-06-30
by Md. Sajedul Karim

Original Post

Original - Posted on 2018-05-16
by Md. Sajedul Karim



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Using EntityManager you can achieve this .
Suppose your entity class is like bellow:
import javax.persistence.*; import java.math.BigDecimal;
@Entity @Table(name = "USER_INFO_TEST") public class UserInfoTest { private int id; private String name; private String rollNo;
public UserInfoTest() { }
public UserInfoTest(int id, String name) { this.id = id; this.name = name; }
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "ID", nullable = false, precision = 0) public int getId() { return id; }
public void setId(int id) { this.id = id; }
@Basic @Column(name = "name", nullable = true) public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Basic @Column(name = "roll_no", nullable = true) public String getRollNo() { return rollNo; }
public void setRollNo(String rollNo) { this.rollNo = rollNo; } }
And your query is "**select id, name from users where roll_no = 1001**".
Here query will return an object with id and a name column. Your Response class is like below:
Your Response class is like:
public class UserObject{ int id; String name; String rollNo;
public UserObject(Object[] columns) { this.id = (columns[0] != null)?((BigDecimal)columns[0]).intValue():0; this.name = (String) columns[1]; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getRollNo() { return rollNo; }
public void setRollNo(String rollNo) { this.rollNo = rollNo; } }
here UserObject constructor will get an Object Array and set data with the object.
public UserObject(Object[] columns) { this.id = (columns[0] != null)?((BigDecimal)columns[0]).intValue():0; this.name = (String) columns[1]; }
Your query executing function is like bellow :
public UserObject getUserByRoll(EntityManager entityManager,String rollNo) { String queryStr = "select id,name from users where roll_no = ?1"; try { Query query = entityManager.createNativeQuery(queryStr); query.setParameter(1, rollNo); return new UserObject((Object[]) query.getSingleResult()); } catch (Exception e) { e.printStackTrace(); throw e; } }
Here you have to import bellow packages:
import javax.persistence.Query; import javax.persistence.EntityManager;
Now your main class, you have to call this function. First get EntityManager and call this `getUserByRoll(EntityManager entityManager,String rollNo)` function. Calling procedure is given below:
**Here is the Imports**
import javax.persistence.EntityManager; import javax.persistence.PersistenceContext;
get `EntityManager` from this way:
@PersistenceContext private EntityManager entityManager; UserObject userObject = getUserByRoll(entityManager,"1001");
Now you have data in this userObject.

**Note**:
`query.getSingleResult()` return a object array. You have to maintain the column position and data type with query column position.
**select id,name from users where roll_no = 1001**
query return a array and it's [0] --> id and [1] -> name.
More info visit [this thread][1] .



[1]: https://stackoverflow.com/a/50365522/3073945
Suppose your query is "***select id,name from users where rollNo = 1001***".
Here query will return a object with id and name column. Your Response class is like bellow:
public class UserObject{ int id; String name; String rollNo; public UserObject(Object[] columns) { this.id = (columns[0] != null)?((BigDecimal)columns[0]).intValue():0; this.name = (String) columns[1]; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRollNo() { return rollNo; } public void setRollNo(String rollNo) { this.rollNo = rollNo; } }
here `UserObject` constructor will get a Object Array and set data with object.
public UserObject(Object[] columns) { this.id = (columns[0] != null)?((BigDecimal)columns[0]).intValue():0; this.name = (String) columns[1]; }
Your query executing function is like bellow :
public UserObject getUserByRoll(EntityManager entityManager,String rollNo) { String queryStr = "select id,name from users where rollNo = ?1"; try { Query query = entityManager.createNativeQuery(queryStr); query.setParameter(1, rollNo); return new UserObject((Object[]) query.getSingleResult()); } catch (Exception e) { e.printStackTrace(); throw e; } }
Here you have to import bellow packages:
import javax.persistence.Query; import javax.persistence.EntityManager;

Now your main class, you have to call this function. First you have to get EntityManager and call this `getUserByRoll(EntityManager entityManager,String rollNo)` function. Calling procedure is given bellow:
@PersistenceContext private EntityManager entityManager; UserObject userObject = getUserByRoll(entityManager,"1001");
Now you have data in this userObject.
**Here is Imports**
import javax.persistence.EntityManager; import javax.persistence.PersistenceContext;
**Note:**
**query.getSingleResult()** return a array. You have to maintain the column position and data type.
select id,name from users where rollNo = ?1 query return a array and it's `[0] --> id and [1] -> name`.
For more info, visit [this Answer][1]
Thanks :)

[1]: https://stackoverflow.com/a/50365522/3073945

        
Present in both answers; Present only in the new answer; Present only in the old answer;