Can I use both #data and #builder annotations in one class Lombok? - java

The reason I want to use is because I want to generate setters in this format
For the class
public class Person {
private String firstName;
private String lastName;
}
public Person setFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public Person setLastName(String lastName) {
this.lastName = lastName;
return this;
}
Instead of the #Data generated setters
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
Or is there any other way? Please let me know.

For it, you should use #Accessors(chain = true). For example:
#Accessors(chain = true)
#Setter
public class Person {
private String firstName;
private String lastName;
}
Vanilla java representation:
public class Person {
private String firstName;
private String lastName;
public Person setFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public Person setLastName(String lastName) {
this.lastName = lastName;
return this;
}
}

Related

SonarQube issue resolve using #NotNull and #JsonProperty together

I'm working on sonarQube issues in my company, on previously developed module I've to modify variable names as a part of fix. So I've applied #JsonProperty but I can't remove #NotNull as well. So my problem is #JsonProperty doing it's job but not null validation failing (THROWING EXCEPTION). Please help me with solution should I apply both annotation's. We are using spring mvc, and restful call to this dto. For now I don't want to make any validation in controller.
public class CustomerImpl extends Customer {
#NotNull(message = "should not be null")
#JsonProperty("customer_Id")
private int customerId;
#NotNull(message = "should not be null")
#JsonProperty("first_name")
private String firstName;
#NotNull(message = "should not be null")
#JsonProperty("last_name")
private String lastName;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
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;
}
}

column alias in hibernate projections

similar questions have been asked before, but not answered yet, i want to perform simple query as follow
select first_name as name from hr_employee
I need to alias column "first_name" as "name"
this is my controller
`public #ResponseBody List EmployeeJson()
{
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
List list = session.createCriteria(HrEmployee.class)
.add(Restrictions.eq("employeeId", 1))
.setProjection(Projections.projectionList()
.add(Projections.property("firstName"), "name") )
.setResultTransformer(Transformers.aliasToBean(HrEmployee.class)).list();
return list;
}`
by running code we get "could not resolve property: name" as column defined in bean class is "first_name" not "name".
`
#Table(name = "hr_employee")
#Entity
#JsonIgnoreProperties({"hibernateLazyInitializer"})
public class HrEmployee {
#Column(name="first_name")
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
`
You have to make to setter and getter methods for name like:
public void setName(String name){this.firstName = name;}
public String getName(){return this.firstName;}
there is no need to make variable "name"
your bean class shoul be like this:
#Table(name = "hr_employee")
#Entity
#JsonIgnoreProperties({"hibernateLazyInitializer"})
public class HrEmployee {
#Column(name="first_name")
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setName(String name){
this.firstName = name;
}
public String getName(){
return this.firstName;
}
}
then the Transformer will call the method setName();

Adding arraylist object to combobox [duplicate]

This question already has answers here:
How do I populate a JComboBox with an ArrayList?
(8 answers)
Closed 6 years ago.
I've created a JCombobox using Netbeans drag and drop.
I have an ArrayList<Person>.
How do I automatically add the FirstName of the Person into the combobox.
The code generated by Netbeans cant be edited in Source view.
Step 1: Lets say you have the following Person class.
Person.java
public class Person {
private int id;
private String firstName;
private String lastName;
public Person() {
}
public Person(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int 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;
}
#Override
public String toString() {
return firstName;
}
}
Step 2: Create the instance of JComboBox and set the model.
java.util.List<Person> list=new java.util.ArrayList<Person>();
list.add(new Person(1, "Sanjeev", "Saha"));
list.add(new Person(2, "Ben", "Yap"));
JComboBox<Person> comboBox = new JComboBox<Person>();
comboBox.setModel(new DefaultComboBoxModel<Person>(list.toArray(new Person[0])));
Step 3: Run your program.
public class PersonBox{
List<Person> person= new ArrayList<Person>();
JCombobox box; //=new JCombobox(...) ?
//used to add a new Person to the box
public void addPerson(Person person){
person.add(person);
/*
*gets the lass element in the list and adds the first
*name of this specific element into the box
*/
box.addItem(person.get(person.size()-1).getFirstName());
}
}
public class Person{
String firstName,sureName;
public Person(String firstName, String sureName){
this.firstName = firstName;
this.sureName = sureName;
}
public String getFirstName(){
return this.firstName;
}
public String getSureName(){
return this.sureName;
}
}

Class mypack.pages.User has been transformed and may not be directly instantiated

I am trying to display a grid with tapestry based on this Tutorial, but i'm getting this error Class mypack.pages.User has been transformed and may not be directly instantiated
those are my classes User
public class User {
#NonVisual
private long id;
private String firstName;
private String lastName;
private int age;
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 int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public User(long id, String firstName, String lastName, int age) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
}
Bellilpage.java
public class Bellilpage {
#Property
private User user;
public List<User> getUsers() {
List<User> dd= new ArrayList<User>();
for(int x=0;x<1;x++)
{
Random rand = new Random();
long d= rand.nextInt(50);
User myuser = new User(d, "Name N° "+d, "lastName N "+d, (int) (d+15));
dd.add(myuser);
}
return dd; }
}
and finally this is how i try to display the grid in the web page
Bellilpage.tml
<html t:type="layout" title="tapestrythetest Index"
t:sidebarTitle="Framework Version"
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
xmlns:p="tapestry:parameter">
<!-- A Zone is a component that can be updated in place, triggered by other components. -->
<t:zone t:id="zone">
<h1>List Users</h1>
<t:grid source="users" row="user">
<p:lastNameCell>
${user.lastname}
</p:lastNameCell>
</t:grid>
</t:zone>
<p:sidebar>
</p:sidebar>
</html>
Why am i gtting this error when i open Bellilpage.tml?
You are getting the error because mypack.pages is a T5 controlled package. Move your User class to a different package, e.g. to mypack.entities. More info at Component Classes, specifically the Component Packages section.

Newbie need help understanding java code

I'm very new to the java programming language and I would really like some help understanding what the following code is doing. I have a pretty decent understanding of what is going on within the Main class. My problem is what part "this._" plays within the code. How exactly are the names getting transferred? This is not homework just self study. The exercise can be found here:http://www.learnjavaonline.org/Functions Also, suggested reading would be great! Thanks!
class Student {
private String firstName;
private String lastName;
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void printFullName(){
System.out.println(this.firstName+" "+this.lastname);
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[] {
new Student("Morgan", "Freeman"),
new Student("Brad", "Pitt"),
new Student("Kevin", "Spacey"),
};
for (Student s : students) {
s.printFullName();
}
}
}
this references to the object your is working in.
so in your sample
class Student {
private String firstName;
private String lastName;
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void printFullName(){
System.out.println(this.firstName+" "+this.lastname);
}
}
this.firstName is the private String firstName; value in your object/class
and firstName is the method parameter.
the this is required in this example as it otherwise would be firstName = firstName and that would assign the value of your parameter to itself.
The reason this is used is because the variables firstName and lastName are shadowed by the constructor parameters. See the differences with this:
class Student {
private String firstName;
private String lastName;
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Compared to without this:
class Student {
private String myFirstName;
private String myLastName;
public Student(String firstName, String lastName) {
myFirstName = firstName;
myLastName = lastName;
}
You use this to reference variables in the current object.
See that variables with "this" are in constructor. This means THE OBJECT, so in the lines:
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
you assing variable to your object. Remember that these variables are in constructor !

Categories