Let's take examples: I have a registration form for a student which has following fields:
Name
Address
Email
Password
I want to insert some student details (name, add, email) in student table, and email, password in login table.
I am using iBatis.
How to achive this....?
Assuming you have a user class like follow
class User{
private String name;
private String add;
private String email;
private String password;
//getter setter
}
your mybatis mapper file should have something like this
<insert id="insertInUser" parameterType="User">
insert into user(name,add,email) values(#{name},#{add},#{email})
</insert>
<insert id="insertInLogin" parameterType="User">
insert into login(email,password) values(#{email},#{password})
</insert>
Related
I am using a entity class for mixing two/three table columns in one entity to hold an outcome of SYS_REFCURSOR in oracle
This allows me to have single class which is not mapped to any table but it still is an Entity
#Data
#Entity
#NoArgsConstructor
class EmployeeDetails {
#Id
#Column("emp_id")
String empId;
#Column("job_name")
String jobName;
#Column("dept_name")
String deptName;
//Future requirement
//String updatedBy
}
Now I have an additional requirement, to add who last modified the employee table, I don't want modify the procedure now, the procedure is being re-used in another background procedure and batch jobs.
My question is, is it possible to use #ManyToOne on this class which is obviously not mapped to any table
If not how do avoid manually looping a child array list, is there a ready made option in JPA or spring boot to achieve that.
Or what will be the smartest/recommended way to bring the below Entity into this class
#Data
#Entity
#NoArgsConstructor
#Table(name="app_users")
class AppUsers {
#Id
#Column(name="user_id")
String userId;
#Column
String userName;
}
#Transient, check how this annotation works it will resolve the issue, you need to understand working of #Transient
My spring boot 2.6.2 EntityManager code is as follows
q = em.createStoredProcedureQuery("MY_PROC",EmployeeDetails.class);
q.registerStoredProcedureParameter("OUT_REFC", void.class, ParameterMode.REF_CURSOR);
q.execute();
q.getResultList()
I have modified my class EmployeeDetails as below
#Data
#Entity
#NoArgsConstructor
class EmployeeDetails {
#Id
#Column("emp_id")
String empId;
#Column("job_name")
String jobName;
#Column("dept_name")
String deptName;
#OneToOne
#JoinColumn(
name="user_id",
referencedColumnName="emp_id",
insertable=false,
updatable=false,
nullable=true
)
AppUsers updatedBy;
}
The log prints Hibernate two times one after one as below, first it calls the proc and then it calls the select query, so, I did not wrote that SQL myself, the JPA layer is taking care of it
Hibernate:
{call MY_PROC(?)}
Hibernate:
select
...
...
from app_users
where user_id=?
so, my expectation achieved and I am getting the values
Not able to create query findByUserId(Long userId)
I have tried
findByUserId(Long user_id)
findByuser_id(Long user_id)
findByUser_id(Long user_id)
Your JPA method, as currently named, expects a user ID variable name of userId, in camel case. Use this definition:
#Column(name="user_id")
private Long userId;
Then use findByUserId(Long userId) as the JPA repo method name as you were already doing. Note that the #Column annotation directs JPA/Hibernate to create the table column with the name user_id.
I am beginner in spring. I want to show SQL data to JSP view page.
This is my SQL table
create table customer(
id int primary key,
name varchar(250),
salary int,
manager_id int
)
and I am trying to show data from this query
select m.id, m.name, m.salary, n.name from customer m, customer n where n.id=m.manager_id
So basically from this query, I am trying to show ID int, name varchar, salary int, manager_name varchar.
I have create the entity java class as below
#Entity
#Table(name="customer")
public class Customer{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column
private int id;
#Column
private String name;
#Column(name="manager_id")
private String manager;
#Column(name="salary")
private int salary;
............
............
}
This is the code of my DAO class
Session session= entityManager.unwrap(Session.class);
Query<Employee> query= session.createQuery(<above sql query need to add here?>,Customer.class);
return query.getResultList();
So the issues are,
This SQL query return the data which could not be matched to Customer Entity class. So do I need to create another Entity class for this? Is there any better way?
The above required SQL query is not able to execute. What the correct way to execute custom SQL query?
Regarding your first question: Yes there is better way. you can directly fetch the results in a projection dto class. take a at: https://vladmihalcea.com/the-best-way-to-map-a-projection-query-to-a-dto-with-jpa-and-hibernate/
About your second question: What Do you mean by saying the query does not execute?
Can you give us the Exception/Stacktrace? Did you try to execute the query (sql form of it) physically? Does it run then?
I have a user stored in oracle DB and one of the fields is stored as a CLOB(a simple Json {"profile": "man"}). I am using Mybatis and i try to retrieve the value.
So i am having:
<resultMap id=userResults>
<property="details" column="DETAILS" jdbcType="CLOB"
javaType="String"
typeHandler="org.apache.ibatis.type.ClobTypeHandler"
</resultMap>
and in the POJO:
the field details as a String with getter and setter.
class User{
private String name;
private String surname;
private String details;
//getters + setters
}
But nothing mapped in the end, even though the row exists in the DB.
The query is:
Select * FROM USER Where USER.id = #{id}
Any recommendations?
After some conversation on comments it turns out that the problem was the Oracle JDBC Driver after suggesting it and OP upgrading it He was able to make it work.
Hi i want to create DAO unit test in SPRING mvc for example for this type of code
package users;
public interface UserDAO {
public void setDataSource(DataSource ds);
public void create(int id, int personal, String password, String first_name, String last_name, String role,
String email, Date date, int id_team);
public User getUser(Integer user_id);
public List<User> listUsers();
void create1(int id, int personal, String password, String first_name, String last_name, String role, String email,
Date start_date);
}
...what is the best way to do it
For simpler DAO methods, which don't use proprietary sql functions you can use in memory relational database, like HSQLDB.
You configure it as a datasource for your tests.
You can start and populate it during test set up and the data will not be persisted anywhere after the test is done.
Seeing the methods you want to test, I suggest you take a look at Spring Data. All these methods are already implemented for you, all you have to do is configure and set up the interface