Relationships question in hibernate - java

I'm learning Hibernate and Play framework (also add Java into account...). I'm having problems saving this kind of entity
#Entity
#Table(name="users")
public class User extends Model {
#Required
public String username;
#Column(name="user_displayname",nullable=true)
public String displayname;
#Password
public String user_password;
#Email
#Column(name="user_email",nullable=false,unique=true)
public String user_email;
public String user_salt;
public Date user_joindate;
#ManyToOne
#JoinTable(name="users_meta")
public UserMeta userdata;
#Required
public boolean user_isActive;
#OneToOne(targetEntity=UserPhotos.class,cascade=CascadeType.ALL)
#JoinColumn(name="id",referencedColumnName="userID")
public UserPhotos userPhoto;
#ManyToMany(cascade=CascadeType.ALL)
#JoinTable(name="links_rol2user")
public List<Rol> rol;
public User (String username, models.Pass password, String user_email) {
this.username = username;
this.user_password = password.getHashedPassword();
this.user_salt = password.getUserHash();
this.user_email = user_email;
this.user_joindate = new Date();
this.user_isActive = false;
}
This is my code when I'm registering a user
// check if the validation has errors
if(validation.hasErrors()) {
params.flash(); // add http parameters to the flash scope
validation.keep(); // keep the errors for the next request
register();
} else {
Cache.delete(uuid);
Pass pass = new Pass(password,new Date().toString());
User newUser = new User(firstName, pass, email);
UserMeta utest = new UserMeta(newUser.id);
utest.setUserTownID(pueblos);
newUser.setUserMeta(utest);
newUser.save();
Logger.info("NewUser ID : %s", newUser.getId());
// UserMeta userInfo = new UserMeta(newUser.getId());
// userInfo.setUserTownID(pueblos);
// userInfo.save();
// TODO salvar foto a null
// Confirmation left
Cache.set("thankyou", "alright!", "3mn");
thankyou();
}
I'm trying to save the userMeta, it does creates a new record when I set the userMeta object into newUser (not visible right now), but it doesn't insert the new ID created in newUser.
What kind of relation do I need? before I tweaked the code as it is now, it was a OneToOne relationship, worked quite well, but now when I was completing the register functions it kinda hit me that I needed to save userMeta object too..
If you need more info let me know, I don't know if I explained it well or not, just trying to get the hang of how Hibernate do relations, etc.
Adding UserMeta:
*/
#Entity
#Table(name="users_meta")
public class UserMeta extends Model {
#Lob
#Column(name="userBio")
public String userBio;
#Column(name="userPhotoID",nullable=true)
public Long userPhotoID = null;
#Column(name="userRoleID", nullable=false)
public Long userRoleID = 2L;
#Lob
public String userDescription;
#Column(name="userViews", nullable=false)
public Long userViews = 0L;
#Column(name="userFavoriteCount", nullable=false)
public Long userFavoriteCount = 0L;
#Column(name="userTotalComments", nullable=false)
public Long userTotalComments = 0L;
#Column(name="userTotalUploadedVideos", nullable=false)
public Long userTotalUploadedVideos = 0L;
public Long userTownID;
public Long userID;
public UserMeta() {}
public UserMeta(Long userid) {
this.userBio = "El usuario no ha escrito nada todavia!";
this.userDescription = "El usuario todavia no se ha describido!";
this.userID = userid;
}
public Long getUserTownID() {
return userTownID;
}
public void setUserTownID(Long userTownID) {
this.userTownID = userTownID;
}
}
// pass model
public class Pass {
protected String hashed;
protected String userHash;
public Pass(String passwordToHash, String salt) {
StringBuffer passSalt = new StringBuffer(passwordToHash);
this.userHash = DigestUtils.md5Hex(salt);
passSalt.append(this.userHash);
passSalt.append(Play.configuration.getProperty("application.passwordSalt"));
this.hashed = DigestUtils.sha512Hex(passSalt.toString());
}
public String getHashedPassword() {
return this.hashed;
}
public String getUserHash() {
return this.userHash;
}
}

There seems to be a lot going on there! But from what I can tell, you problem is with the id that you are passing into the UserMeta.
As you are extending Model, the id is being generated by the Model class. However, this is not set until after the entity is saved to the database (as the id is auto-generated by the database).
Therefore, because you are passing the id into the UserMeta before the User object is saved, the value of id will be null.
If you can save the User object before you create your UserMeta object, your code should work.

Related

How GET a list of files

I need that every time I list a portfolio it returns all the images that exist in that specific portfolio
I can list 1 by 1 via ID but when I send my endpoint to list all photos belonging to the ID of a specific portfolio it only returns me null
Photo Class
#Entity
public class Foto {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String nomeArquivo;
#ManyToOne(cascade = CascadeType.MERGE)
private Perfil autonomo;
#Column(length = 5_000_000)
private byte[] fotoByte;
private String tipoArquivo;
}//Getters and Setters
AutonomoService
#Autowired
FotosRepository fotosRepository;
#Autowired
PerfisRepository perfisRepository;
public List<byte[]> portfolio(int id){
if (perfisRepository.existsById(id)) {
return fotosRepository.findAllByAutonomoId(id).stream().map(f-> f.getFotoByte()).collect(Collectors.toList());
}
else {
return null;
}
} //Getters and Setters
Controller
#GetMapping("/portfolio/fotos/{id}")
public ResponseEntity<List<byte[]>> getPortfolioAutonomo(#PathVariable int id) throws IOException {
List<byte[]> result = autonomoService.portfolio(id);
return ResponseEntity.status(200).body(result);
}
And this is the way I can get 1 photo by its id
#GetMapping("/portfolio/{id}")
public ResponseEntity getPortfolio(#PathVariable int id){
Optional<Foto> anexoOptional = fotosRepository.findById(id);
if (anexoOptional.isPresent()) {
Foto anexo = anexoOptional.get();
return ResponseEntity.status(200)
.header("content-type", anexo.getTipoArquivo())
.header("content-disposition", "filename=" + anexo.getNomeArquivo())
.body(anexo.getFotoByte());
} else {
return ResponseEntity.status(404).build();
}
}
Instead of
return fotosRepository.findAllByAutonomoId(id).stream().map(f-> f.getFotoByte()).collect(Collectors.toList());
Can you try with
return fotosRepository.findAllById(id).stream().map(f-> f.getFotoByte()).collect(Collectors.toList());
If still this is not working, better to go with #Query implemention.

store objects from a third party library in a hibernate database

Before I start, I don't have much experience with hibernate. I'm working on an application where I use my colleagues library to display some data. There are recording objects I want to store in my database, so I don't need to create them every time I use a particular function. I would like to read them from the database. First I tried to do it like this:
#Entity
public class RecordingEntity
{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Lob
#Column(name = "recording")
private Recording recording;
#ManyToOne
#Column(name = "user")
private User user;
public RecordingEntity(Recording recording, User user)
{
this.recording = recording;
this.user = user;
}
public Recording getRecording()
{
return recording;
}
public Long getId()
{
return id;
}
public void setRecording(Recording recording)
{
this.recording = recording;
}
public void setId(Long id)
{
this.id = id;
}
public Long getUser()
{
return user;
}
public void setUser(User user)
{
this.user = user;
}
}
public class ImportRecording()
{
...
try
{
TapestryDBConnection.performAtomic(new DBAtomicRunnable()
{
#Override
public void run(Session session) throws InterruptedException, IOException, SerialException, SQLException
{
...
RecordingEntity recordingEntity = new RecordingEntity(recording);
session.saveOrUpdate(recordingEntity);
...
}
}, true);
}
return null;
}
This would lead to: java.lang.ClassCastException: Recording cannot be cast to java.sql.Blob
Then I tried it without the #Lob and I got:
org.hibernate.MappingException: Could not determine type for: Recording, at table: RecordingEntity, for columns: [org.hibernate.mapping.Column(recording)]
EDIT:
I have tried to save the object with converting them into byte[] and storing the byte[] on to the database. Unfortunately it is not possible unless the recording class implements the interface Serializable. I would prefer to solve this problem without implementing Serializable.

Null columns are created on either tables when accessing data using Spring Data JPA

I am new to Spring Data JPA and Hibernate. By looking at different examples I built a working model for CRUD operations on one entity, I am having trouble in joining two tables to extract AF_NAME using AF_ID from another table which is Foreign key. A null column is created with the names of and while accessing, null is returned.please check if I am following preocedure for joins and point me to any tutorial know.
I followed this solution and still there is no progress.
#Entity
#Configuration
#EnableAutoConfiguration
#Table(name = "AFF_CONFIG")
public class AFF_CONFIG implements Serializable {
#Id
#Column(name = "AFF_CONFIG_ID")
private String AFF_CONFIG_ID;
#Column(name = "AFF_ID")
private String AFF_ID;
#Column(name = "CH_ID")
private String CH_ID;
#Column(name = "M_ID")
private Long M_ID;
#Column(name = "KEY")
private String KEY;
#Column(name = "VALUE")
private String VALUE;
#Column(name = "SYSTEM")
private String SYSTEM;
private AFF aff;
#LazyCollection(LazyCollectionOption.TRUE)
#ManyToOne
#JoinColumn(name = "AFF_ID")
public AFF getAff() {
return aff;
}
public void setAffiliate(AFF aff) {
this.aff = aff;
}
public String getAFF_CONFIG_ID() {
return AFF_CONFIG_ID;
}
public void setAFF_CONFIG_ID(String aFF_CONFIG_ID) {
AFF_CONFIG_ID = aFF_CONFIG_ID;
}
public String getAFF_ID() {
return AFF_ID;
}
public void setAFF_ID(String aFF_ID) {
AFF_ID = AFF_ID;
}
public String getCH_ID() {
return CH_ID;
}
public void setCHANNEL_ID(String cH_ID) {
CH_ID = cH_ID;
}
public Long getM_ID() {
return M_ID;
}
public void setM_ID(Long m_ID) {
M_ID = m_ID;
}
public String getKEY() {
return KEY;
}
public void setKEY(String kEY) {
KEY = kEY;
}
public String getVALUE() {
return VALUE;
}
public void setVALUE(String vALUE) {
VALUE = vALUE;
}
public String getSYSTEM() {
return SYSTEM;
}
public void setSYSTEM(String sYSTEM) {
SYSTEM = sYSTEM;
}
Second entity is:
#Entity
#Table(name = "AFF")
public class AFF implements Serializable {
#Column(name = "AFF_NAME")
private String AFF_NAME;
#Column(name = "AFF_CODE")
private String AFF_CODE;
#Id
#Column(name = "AFF_ID")
private String AFF_ID;
private Set<AFF_CONFIG> someAff = new HashSet<AFF_CONFIG>();
#LazyCollection(LazyCollectionOption.TRUE)
#OneToMany(cascade = CascadeType.ALL, mappedBy = "aff")
public Set<AFF_CONFIG> getSomeAff() {
return someAff;
}
public void setSomeAff(Set<AFF_CONFIG> someAff) {
this.someAff = someAff;
}
public String getAFF_ID() {
return AFF_ID;
}
public void setAFF_ID(String aFF_ID) {
AFF_ID = aFF_ID;
}
public String getAFF_NAME() {
return AFF_NAME;
}
public void setAFF_NAME(String aFF_NAME) {
AFF_NAME = aFF_NAME;
}
public String getAFF_CODE() {
return AFF_CODE;
}
public void setAFF_CODE(String aFF_CODE) {
AFF_CODE = aFF_CODE;
}
Since this is many to one relation I created set type in one and object type in another as defined in other places.Created a repository by extending crud and added a query. Excise the bunch of different annotations, I included them in hoping to solve the null entry.
#Repository
public interface MarketRepository extends CrudRepository<AFF_CONFIG,String> {
Page<AFF_CONFIG> findAll(Pageable pageable);
#Query("Select a,b from AFF_CONFIG a, AFF b where a.AFF_ID = b.AFF_ID" )
public List<AFF_CONFIG> getAffData();
}
the applicatoin is working fine even after some tinkering until I Included these annotations. Now there is this error:
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: aff.
I solved the issue with the help of my Supervisor. Looks like we have to follow naming specifications for Class and variables. And one more correction is to remove collection type object and change it to just object (removed set in aff class).I will post the corrected later, to compare and contrast.

Error while binding form request play-framework 2.5.4

I am using play-framework-java 2.5.4.
I want to bind a user-input-from values to my model class variables.
This is my controller function that except form POST :
public Result formSubmit()
{
MlmModel mlmModel;
play.data.Form<MlmModel> form = play.data.Form.form(MlmModel.class).bindFromRequest();
mlmModel = form.get();
mlmModel.save();
mlmModel.callUpdate();
return ok(Json.toJson(mlmModel));
}
But I am getting this error
[CompletionException: java.lang.IllegalStateException: Error(s) binding form: {"parent_id":["Invalid value"]}]
because, one of my input field is not compulsory to fill by user.
It's fine if user left blank some fields.
But my code should
save all data that user provides to database and
should save 0 by default for the fields left blank by user .
I am Using play-Eben and my database server is MySQL 5.x
How can I achieve this?
Edit 1 :
MlmModel.java
#Entity
public class MlmModel extends Model
{
#Id
#GeneratedValue()
public Long ID;
public Logic logic;
public MlmModel() {
this.parent_id = 0;
logic = new Logic(this);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String name;
public long getParent_id() {
return parent_id;
}
public void setParent_id(long parent_id) {
if (String.valueOf(parent_id).isEmpty())
this.parent_id = 0;
this.parent_id = parent_id;
}
#Column(columnDefinition = "long default 0")
public long parent_id;
#Formats.DateTime(pattern="dd/MM/yyyy")
public Date created_time = new Date();
public Long balance = new Long(0);
public static Finder<Long, MlmModel> find = new Finder<Long,MlmModel>(MlmModel.class);
public List<ValidationError> validate()
{
List<ValidationError> errors = new ArrayList<ValidationError>();
if (Long.toString(parent_id).isEmpty())
parent_id = 0;
if (errors.isEmpty())
return null;
else
return errors;
}
}

JSF 2.0 "Conversion Error setting value 'courseID' for 'null Converter'.!!How can I avoid this kinda error? [duplicate]

This question already has answers here:
Conversion Error setting value for 'null Converter' - Why do I need a Converter in JSF?
(2 answers)
Closed 7 years ago.
I am working on a web application using PrimeFaces, JPA, Hibernate and JSF 2.0.
I have two entities Students and Course with an many to one relationship.
#Entity
#Table(name = "STUDENTS")
public class Students extends MainUsers implements Serializable {
private String firstName;
private String lastName;
private long mobile;
private String jobTitle;
private int duration;
#Temporal(TemporalType.TIMESTAMP)
private Date startDate;
#Temporal(TemporalType.TIMESTAMP)
private Date endDate;
#ManyToOne
#JoinColumn(name = "course_id", nullable = true)
private Course company;
// Getters and setters.
}
#Entity
#Table(name = "COURSE")
#NamedQueries({
#NamedQuery(name = "course.findCourseByCourseId", query = "select c from Course c where c.Id =:id"),
#NamedQuery(name = "course.findCourseByCourseName", query = "select c from Course c where c.courseName =:courseName") })
public class Course implements Serializable {
public static final String FIND_COURSE_BY_COURSE_ID = "course.findByCourseId";
public static final String FIND_COURSE_COURSE_NAME = "course.findByCourseName";
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int Id;
private String courseName;
#OneToMany(targetEntity = Students.class, mappedBy = "course", fetch = FetchType.LAZY)
private List<Students> students;
// Getters and setters.
#Override
public int hashCode() {
return getId();
}
#Override
public boolean equals(Object obj) {
if (obj instanceof Course) {
Course course = (Course) obj;
return course.getId() == Id;
}
return false;
}
}
I have a registration page where it takes student's names, email, password and course. For selecting the course I have used this
<h:outputText value="Course :" />
<p:selectOneMenu value="#{courseMB.course}">
<f:selectItem itemLabel="Select one Course" itemValue="" />
<f:selectItems value="#{courseMB.allCourses}" var="crs"
itemLabel="#{crs.courseName}" itemValue="#{crs.id}" />
<f:converter converterId = "courseConverter"/>
</p:selectOneMenu>
To create a student I have the following method in StudentMB class:
public void createStudent() {
String test = student.getEmail();
if (getStudentFacade().isStudentExist(test)){
System.out.println(test);
} else {
try {
student.setActivation_key(hashKey());
student.setRole(Role.STUDENT);
student.setActive(0);
getStudentFacade().createStudent(student);
displayInfoMessageToUser("Created with success");
SendEmail.Send(username, password, student.getEmail(), title, linkToSend());
loadStudent();
resetStudent();
} catch (Exception e) {
displayErrorMessageToUser("Opps, we could not create the user. Try again");
e.printStackTrace();
}
}
}
I don't really know how to get the course into StudentMB and persist that into the database. The current methodology gives me an error
Conversion Error setting value 'courseID' for 'null Converter'.
I tried to use a converter but I am getting empty string into the argument.
#FacesConverter(value="courseConverter", forClass = com.model.Course.class)
public class CourseConverter implements Converter {
#Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
CourseFacade courseFacade = new CourseFacade();
int courseId;
try {
courseId = Integer.parseInt(submittedValue);
} catch (NumberFormatException exception) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Type the name of a Course and select it (or use the dropdown)",
"Type the name of a Course and select it (or use the dropdown)"));
}
return courseFacade.findCourse(courseId);
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
Course course = null;
if (modelValue instanceof Course){
course = (Course) modelValue;
StringBuilder asString = new StringBuilder();
asString.append(course.getId()+"-");
asString.append(course.getCourseName());
return asString.toString();
}
return "";
}
}
How is this caused and how can I solve it?
You need to set the select item value to the Course itself, not to its id.
Replace
itemValue="#{crs.id}"
by
itemValue="#{crs}"
Otherwise the converter would only get the #{crs.id} as modelValue in getAsString() and thus return an empty string. This empty string is in turn submitted back to the server through getAsObject() which in turn causes the exception.
You should have thrown a ConverterException in the else part of the if (modelValue instanceof Course) so that it was immediately clear that the converter isn't suitable for the given model value.
See also:
Our selectOneMenu wiki page

Categories

Resources