a simple save in spring boot jpa / hibernate saves a row of nulls in the database.
package rest.api.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import java.sql.Blob;
import java.sql.Date;
#Entity
#Table(name="user")
public class User {
#Id
#GeneratedValue(strategy= GenerationType.AUTO)
#Column(name="id")
private #JsonIgnore Long id;
#Column(name="created")
private Date created;
#Column(name="latitude")
private #JsonIgnore
Float latitude;
#Column(name="longitude")
private #JsonIgnore Float longitude;
#Column(name="password")
private #JsonIgnore String password;
#Column(name="user_name")
private #JsonIgnore String username;
#Column(name="pic_thumbnail")
private #JsonIgnore Blob picThumbnail;
#Column(name="first_name")
private #JsonIgnore String firstName;
#Column(name="middle_name")
private #JsonIgnore String middleName;
#Column(name="last_name")
private #JsonIgnore String lastName;
#Column(name="location_of_residence")
private #JsonIgnore String locationOfResidence;
#Column(name="telephone")
private #JsonIgnore String telephone;
#Column(name="email")
private #JsonIgnore String email;
#Column(name="description")
private #JsonIgnore String description;
#Column(name="date_of_birth")
private #JsonIgnore
java.sql.Date dob = null;
protected User () {}
public User(Long id, Date created, Float latitude, Float longitude, String password, String username, Blob picThumbnail, String firstName, String middleName, String lastName, String locationOfResidence, String telephone, String email, String description, Date dob) {
this.id = id;
this.created = created;
this.latitude = latitude;
this.longitude = longitude;
this.password = password;
this.username = username;
this.picThumbnail = picThumbnail;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.locationOfResidence = locationOfResidence;
this.telephone = telephone;
this.email = email;
this.description = description;
this.dob = dob;
}
// constructor for Logindetails
public User(Long id, String username, String password, String email) {
this.id = id;
this.created = null;
this.latitude = null;
this.longitude = null;
this.password = password;
this.username = username;
this.picThumbnail = null;
this.firstName = null;
this.middleName = null;
this.lastName = null;
this.locationOfResidence = null;
this.telephone = null;
this.email = email;
this.description = null;
this.dob = null;
}
public User(String username, String email, String password) {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Float getLatitude() {
return latitude;
}
public void setLatitude(Float latitude) {
this.latitude = latitude;
}
public Float getLongitude() {
return longitude;
}
public void setLongitude(Float longitude) {
this.longitude = longitude;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Blob getPicThumbnail() {
return picThumbnail;
}
public void setPicThumbnail(Blob picThumbnail) {
this.picThumbnail = picThumbnail;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLocationOfResidence() {
return locationOfResidence;
}
public void setLocationOfResidence(String locationOfResidence) {
this.locationOfResidence = locationOfResidence;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
}
#Repository
public interface UserRepository extends PagingAndSortingRepository {
}
package rest.api.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import rest.api.data.repository.UserRepository;
import rest.api.entity.User;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.methodOn;
#RestController
#Service
#Transactional
public class UserController {
private final UserRepository userDao;
#Autowired
public UserController(UserRepository userDao) {
this.userDao = userDao;
}
#Transactional
#RequestMapping(value = "/users/{user-id}", method = RequestMethod.GET)
public ResponseEntity<Resource<User>> getSpecificModel(#PathVariable(value = "user-id") Long userId) {
User specificUser = userDao.findOne(userId);
Resource<User> resource = new Resource<>(specificUser, linkTo(methodOn(MessageController.class)
.getSpecificModel(userId)).withSelfRel());
return ResponseEntity.ok(resource);
}
#Transactional
#RequestMapping(value = "/users/{user-id}", method = RequestMethod.DELETE)
public long remove(#PathVariable(value = "user-id") long userId) {
userDao.delete(userId);
return userId;
}
#RequestMapping(value = "/users", method = RequestMethod.POST)
public void registerLoginDetails(#RequestParam(name="username") String username,
#RequestParam(name="email") String email,
#RequestParam(name="password") String password) {
User loginDetails = new User(username, email, password);
userDao.save(loginDetails);
}
#Transactional
#RequestMapping(value = "/profile", method = RequestMethod.POST)
public void registerPersonalDetails() {
}
}
Every column in my users table can take a null, and so it is allowed to save a record in this state. I checked this by executing the equivalent query against the databse manually.
Does anyone have any ideas?
One glaring error is that the constructor in User used to register a user doesn't assign the username, email, password parameters passed in.
Related
I want to show it in my response distance field, but I am not able to do it please help.
Here is my repo class in alias name (distance) I need to attach this distance column with my list of user results.
Repository
String countDistanceQuery = "(3959 * ACOS(COS(RADIANS(:latitude)) * COS(RADIANS(latitude)) * COS(RADIANS(longitude) - RADIANS(:longitude)) + SIN(RADIANS(:latitude)) * SIN(RADIANS(latitude))))";
#Query(value = "SELECT a.* , " + countDistanceQuery + " AS distance FROM user a WHERE a.address LIKE %:address% HAVING distance < 25 ORDER BY distance LIMIT 0, 20" , nativeQuery = true)
public List<User> getNearsetLoactionOfSitter(String address, String latitude, String longitude);
Model
Here I will take the distance attribute as a #Transient to know the JPA does not present in the database.
package com.misha.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import lombok.ToString;
import javax.persistence.*;
#Entity
#ToString
#Table(name = "user",uniqueConstraints = {
#UniqueConstraint(columnNames = "email")
})
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String contactname;
#Column(unique=true)
private String email;
private String password;
private String company;
private String location;
private String address;
private String latitude;
private String longitude;
private String open;
private String close;
private float chargesperhour;
private String logo;
private boolean enabled;
#Transient
private double distance;
#Column(name = "reset_password_token")
private String resetPasswordToken;
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(name = "users_roles", joinColumns = #JoinColumn(name = "id"), inverseJoinColumns = #JoinColumn(name = "role_id"))
private Set<Role> roles = new HashSet<>();
public User() {
super();
}
public User(Integer id, String contactname, String email, String password, String company, String location, String address,
String latitude, String longitude, String open, String close, float chargesperhour, String logo,
boolean enabled, String resetPasswordToken) {
super();
this.id = id;
this.contactname = contactname;
this.email = email;
this.password = password;
this.company = company;
this.location = location;
this.address = address;
this.latitude = latitude;
this.longitude = longitude;
this.open = open;
this.close = close;
this.chargesperhour = chargesperhour;
this.logo = logo;
this.enabled = enabled;
this.resetPasswordToken = resetPasswordToken;
}
public User(Integer id, String contactname, String email, String password, String company, String location,
String address, String latitude, String longitude, String open, String close, float chargesperhour,
String logo, boolean enabled, double distance, String resetPasswordToken, Set<Role> roles) {
super();
this.id = id;
this.contactname = contactname;
this.email = email;
this.password = password;
this.company = company;
this.location = location;
this.address = address;
this.latitude = latitude;
this.longitude = longitude;
this.open = open;
this.close = close;
this.chargesperhour = chargesperhour;
this.logo = logo;
this.enabled = enabled;
this.distance = distance;
this.resetPasswordToken = resetPasswordToken;
this.roles = roles;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContactname() {
return contactname;
}
public void setContactname(String contactname) {
this.contactname = contactname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getOpen() {
return open;
}
public void setOpen(String open) {
this.open = open;
}
public String getClose() {
return close;
}
public void setClose(String close) {
this.close = close;
}
public float getChargesperhour() {
return chargesperhour;
}
public void setChargesperhour(float chargesperhour) {
this.chargesperhour = chargesperhour;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getResetPasswordToken() {
return resetPasswordToken;
}
public void setResetPasswordToken(String resetPasswordToken) {
this.resetPasswordToken = resetPasswordToken;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
Controller
#GetMapping("/users/search")
public ResponseEntity<List<User>> searchNearestLocation(#RequestParam String address, #RequestParam String latitude, #RequestParam String longitude){
if(!address.isEmpty() && !latitude.isEmpty() && !longitude.isEmpty()) {
try {
List<User> list = userService.getNearsetLoactionOfSitter(address, latitude, longitude);
if( list.isEmpty() || list.size() == 0 ) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<>(list, HttpStatus.OK);
}catch(Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}else {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
I need to add Distance column in my response
I need to show calculated distance in response
#Transient annotation is used to mark a field to be transient for the mapping framework, which means the field marked with #Transient is ignored by mapping framework and the field not mapped to any database column.
Therefore try representing the distance property with #Formula annotation, and apply the distance calculation so that we can obtain the calculated value(I can see that you are calculating the distance based upon the image you have shared).
With #Formula, we can call native database functions and stored procedures and basically do anything that does not break the syntax of an SQL select clause for this field.
Reference: [https://www.concretepage.com/hibernate/formula_hibernate_annotation]
I am a beginner when it comes to API's and JSON serialization. I'm encountering a weird output. It seems that my JSON output is being wrongly formatted? I am using spring boot 1.59 and when I check my result in postman I am getting the following error:
controller
/**
*
* #return
*/
#PostMapping(value = "/getUser")
#ResponseBody
public User getUSer(#RequestParam int userID) {
System.out.println("Request Id is---->"+userID);
User user = userRepository.findById(userID);
return user;
}
User
#Entity
#Table(name = "users")
public class User implements Serializable {
#Id
//#GeneratedValue(strategy = GenerationType.IDENTITY)
#GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
#NotNull
#Size(max = 65)
#Column(name = "first_name")
private String firstName;
#Size(max = 65)
#Column(name = "last_name")
private String lastName;
#NotNull
#Email
#Size(max = 100)
#Column(unique = true)
private String email;
#NotNull
#Size(max = 128)
private String password;
#OneToOne(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "user")
private UserProfile userProfile;
// Hibernate requires a no-arg constructor
public User() {
}
public User(String firstName, String lastName, String email, String password) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
}
// Getters and Setters (Omitted for brevity)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserProfile getUserProfile() {
return userProfile;
}
public void setUserProfile(UserProfile userProfile) {
this.userProfile = userProfile;
}
}
UserProfile
#Entity
#Table(name = "user_profiles")
public class UserProfile implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "phone_number")
#Size(max = 15)
private String phoneNumber;
#Enumerated(EnumType.STRING)
#Column(length = 10)
private Gender gender;
#Temporal(TemporalType.DATE)
#Column(name = "dob")
private Date dateOfBirth;
#Size(max = 100)
private String address1;
#Size(max = 100)
private String address2;
#Size(max = 100)
private String street;
#Size(max = 100)
private String city;
#Size(max = 100)
private String state;
#Size(max = 100)
private String country;
#Column(name = "zip_code")
#Size(max = 32)
private String zipCode;
#OneToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "user_id", nullable = false)
private User user;
public UserProfile() {
}
public UserProfile(String phoneNumber, Gender gender, Date dateOfBirth,
String address1, String address2, String street, String city,
String state, String country, String zipCode) {
this.phoneNumber = phoneNumber;
this.gender = gender;
this.dateOfBirth = dateOfBirth;
this.address1 = address1;
this.address2 = address2;
this.street = street;
this.city = city;
this.state = state;
this.country = country;
this.zipCode = zipCode;
}
// Getters and Setters (Omitted for brevity)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
UserRepository
public interface UserRepository extends JpaRepository<User, Long> {
User findById(int id);
}
As well as a big long stack trace of fasterxml.jackson errors:
[jackson-databind-2.6.6.jar:2.6.6]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:693) ~[jackson-databind-2.6.6.jar:2.6.6]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:675) ~[jackson-databind-2.6.6.jar:2.6.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157) ~[jackson-databind-2.6.6.jar:2.6.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149) ~[jackson-databind-2.6.6.jar:2.6.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:111) ~[jackson-databind-2.6.6.jar:2.6.6]
at
It happened due to infinite recursion between two entities and I solved using JsonManagedReference, JsonBackReference
I'm beginner in jpa and i encountered with composite query.
Then I started search solution. Many people advised use criteria API.
And so the question: how create similar query using criteriaQuery?
MySQL version
SELECT role FROM Role WHERE id_role=(SELECT id_role FROM Client WHERE email=:email)
Role
#Entity
#Table(name="role")
public class Role {
#Id
public int id_role;
private enum enumRole {ADMIN, CLIENT}
#Column(name="role")
private enumRole role;
public Role() {
}
public Role(enumRole role) {
this.role = role;
}
public String getRole() {
return this.role.name();
}
public void setRole(String role) {
this.role = enumRole.valueOf(role);
}
#OneToMany(targetEntity = Client.class)
private List clientList;
}
Client
#Entity
#Table(name ="client")
public class Client {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public Long Id;
#Column(name="first_name", length = 25)
private String firstName;
#Column(name="surname", length = 25)
private String surName;
#Column(name="password", length = 30)
private String password;
#Column(name="email")
private String email;
#Column(name="username", length = 15)
private String username;
public Client() {
}
#ManyToMany(targetEntity = Ticket.class)
private Set ticketSet;
public Client(String firstName, String surName, String password, String email, String username) {
this.firstName = firstName;
this.surName = surName;
this.password = password;
this.email = email;
this.username = username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSurName() {
return surName;
}
public void setSurName(String surName) {
this.surName = surName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
}
Can you not create a mapping between Client and Role? Either a #OneToMany or #ManyToMany? If so then you dont need to write any sql and could do this with a simple JPA Query method http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation
I have spring application everytime I run and I try to login I got the following excpetion after login
java.lang.IllegalStateException: Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)
at com.emc.fleet.domain.User_Roo_Jpa_ActiveRecord.ajc$interMethod$com_emc_fleet_domain_User_Roo_Jpa_ActiveRecord$com_emc_fleet_domain_User$entityManager(User_Roo_Jpa_ActiveRecord.aj:19)
at com.emc.fleet.domain.User.entityManager(User.java:1)
at com.emc.fleet.domain.User_Roo_Jpa_ActiveRecord.ajc$interMethodDispatch1$com_emc_fleet_domain_User_Roo_Jpa_ActiveRecord$com_emc_fleet_domain_User$entityManager(User_Roo_Jpa_ActiveRecord.aj)
at com.emc.fleet.domain.User_Roo_Finder.ajc$interMethod$com_emc_fleet_domain_User_Roo_Finder$com_emc_fleet_domain_User$findUsersByUserIdEquals(User_Roo_Finder.aj:47)
at com.emc.fleet.domain.User.findUsersByUserIdEquals(User.java:1)
I have read many STO questions and checked all answers none of them succeded
this is my user class
package com.emc.fleet.domain
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
#RooJavaBean
#RooToString
#RooJpaActiveRecord(finders = { "findUsersByEmailLike", "findUsersByUserIdEquals", "findUsersByCostCenter", "findUsersByDepartmet" })
public class User {
#Id
#GeneratedValue
private Long id;
#NotEmpty
#NotNull
private String firstName;
#NotEmpty
#NotNull
private String lastName;
#NotNull
private Long userId;
#Email
#NotNull
private String email;
#NotNull
private String address;
#NotNull
private String district;
private String deskPhone;
#NotEmpty
#NotNull
private String mobile;
#NotEmpty
#NotNull
private String password;
#Transient
private String retypePassword;
#OneToOne
private Department departmet;
#OneToOne
#JoinColumn(name = "cost_center")
private CostCenter costCenter;
private String managerName;
private boolean enabled = true;
#Enumerated(EnumType.STRING)
private Roles role = Roles.ROLE_USER;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getDeskPhone() {
return deskPhone;
}
public void setDeskPhone(String deskPhone) {
this.deskPhone = deskPhone;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRetypePassword() {
return retypePassword;
}
public void setRetypePassword(String retypePassword) {
this.retypePassword = retypePassword;
}
public Department getDepartmet() {
return departmet;
}
public void setDepartmet(Department departmet) {
this.departmet = departmet;
}
public CostCenter getCostCenter() {
return costCenter;
}
public void setCostCenter(CostCenter costCenter) {
this.costCenter = costCenter;
}
public String getManagerName() {
return managerName;
}
public void setManagerName(String managerName) {
this.managerName = managerName;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public Roles getRole() {
return role;
}
public void setRole(Roles role) {
this.role = role;
}
#Override
public String toString() {
return getEmail() + " - " + getUserId();
}
}
and this the user_Roo_Configurable file
package com.emc.fleet.domain;
import com.emc.fleet.domain.User;
import org.springframework.beans.factory.annotation.Configurable;
privileged aspect User_Roo_Configurable {
declare #type: User: #Configurable;
}
any clue ?
I am developing an Online Book Library application in struts...
I have a user form wherein user will enter his details like first name,last name etc and also there would be a books list where he will select some books that he wants..an d later on I want to insert those details into 2 tables..i.e user details in obl_users and and books that user select in users_books.
I used the below code for the list ..
<%
if(request.getAttribute("booksNameList") != null) {
%>
<html:select property="displayBooks" multiple="true" size="5">
<logic:iterate id="booksNameList" name="booksNameList" scope="request">
<html:option value="${booksNameList.bookId}" ><bean:write name="booksNameList" property="longTitle" /></html:option>
</logic:iterate>
</html:select>
<%
}
%>
Initially when user request form then the form will have pre populated list will all the books name in database..I am not sure about the code that i wrote to get book id in the value part of i.e value="${booksNameList.bookId}" ..
In my addUser() I want to iterate through the user selected books like this..
for (int i = 0; i < selectedBooks.length; i++) {
//insertBooks.setInt(1, generatedKeys.getInt(1));
//insertBooks.setInt(2, Integer.parseInt(selected[i]));
//insertBooks.addBatch();
}
but for that how to get the user selected books..
Here is my user.java
public class User extends ActionForm {
private int userId;
private String firstName;
private String lastName;
private String middleName;
private String username;
private String password;
private String contactNumber;
private String membershipNumber;
private String role;
private String email;
private String address;
private String comments;
private String dateOfBirth;
private int oblStatus;
private String createdDate;
private String updatedDate;
private String createdBy;
private String updatedBy;
private String displayBooks;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public String getMembershipNumber() {
return membershipNumber;
}
public void setMembershipNumber(String membershipNumber) {
this.membershipNumber = membershipNumber;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public int getOblStatus() {
return oblStatus;
}
public void setOblStatus(int oblStatus) {
this.oblStatus = oblStatus;
}
public String getCreatedDate() {
return createdDate;
}
public void setCreatedDate(String createdDate) {
this.createdDate = createdDate;
}
public String getUpdatedDate() {
return updatedDate;
}
public void setUpdatedDate(String updatedDate) {
this.updatedDate = updatedDate;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public String getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy;
}
public String getDisplayBooks() {
return displayBooks;
}
public void setDisplayBooks(String displayBooks) {
this.displayBooks = displayBooks;
}
}
book bean class:
private String bookId;
private String longTitle;
private String shortTitle;
private String isbn;
private String dateOfPublication;
private String noOfPages;
private String boundType;
private String dvdAvailability;
private String noOfAvailableCopies;
private int oblStatus;
private String createdDate;
private String updatedDate;
private String createdBy;
private String updatedBy;
private String displayAuthors;
private int[] authorIds;
please guide me...i am totally new to struts
You have to put "displayBooks" property as a String array like;
private String[] displayBooks;
Since you want to select multiple values on the form, Array or ArrayList can be the only better option to use.