object oriented properties for new object creation - oop

When we create new object example there is class Employee.Create a instance of Employee class.
Employee emp1 = new Employee();
Which OOPS property this above statement describes

With the statement Employee emp1 = new Employee(); you are instantiating an object of the class Employee and referencing to emp1.
Regards,

Related

Use find() and with() together in Laravel query

I have 2 tables employees and employee_locations. One employee has many locations. I need to find out one employee record with related latest employee_locations record. I wrote below query.
$employees = Employee::find([1])->with('employees.employee_locations')->latest()->first();
I am getting below error
BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::with does not exist.
Your issue is that the find method retrieves a collection of Eloquent objects, on which the with method can not be used. You must first specify your relations for the Employee objects and then use find.
The below code will retrieve the employee with the ids specified in find method and locations for each employee:
$employees = Employee::with('employees.employee_locations')->find([1])
create a relation in your model. Something like this:
class Employee extends Model
{
protected $table = 'employees';
public function location()
{
return $this->hasMany(EmployeeLocation::class, 'employeed_id');
}
}
class EmployeeLocation extends Model
{
protected $table = 'employee_locations';
}
$employees = Employee::with('location')->first();
or you do
$employees = Employee::with('location')->find(<employeed_id>);
Try This method
$employees = Employee::with('employees')->where('employeesid',$employeesid)-
>get()->find($employeesid);

JMockit: alter parameter of a mocked method

Can JMockit modify the parameters of methods that it mocks? It's certainly easy to modify the return value of the method it mocks, but how about modifying the parameters themselves? I know it's possible to at least capture and test the mocked parameters using Verifications, but this happens after the fact.
Here is my simplified code:
class Employee{
Integer id;
String department;
String status;
//getters and setters follow
}
The method I want to test:
public int createNewEmployee() {
Employee employee = new Employee();
employee.setDepartment("...");
employee.setStatus("...");
//I want to mock employeeDao, but the real DAO assigns an ID to employee on save
employeeDao.saveToDatabase(employee);
return employee.getId(); //throws NullPointerException if mocked, because id is null
}
Use a Delegate object assigned to the result field, when recording an expectation on employeeDao.saveToDatabase(...). The delegate method (with an arbitrary name) should declare a Employee emp parameter; then simply call emp.setId(...) with whatever id value you want.
For examples, see the documentation.

Custom table mapping for OData endpoint

I've successfully completed a few examples used to create Web API Odata services. Such as:
Create model class "Employee"
Create controller "EmployeesController"
Use GET /Employees to get list of records
These examples would create a SQL table called "Employees". How would I map this model\controller to an existing SQL table called "corpemployee"?
Thanks
You need to execute the sql statement first and map the result to the Employee object, for example:
public class EmployeesController:ODataController
{
public IHttpActionResult Get()
{
string sql="select * from corpemployee";
// Create a database connection
// Execute the sql statement
IList<Employee> employees=new List<Employee>();
Employee employee=new Employee();
foreach(var result in resultset)
{
Employee employee=new Employee();
emmployee.ID=result["ID"];
// Other properties go here
employees.Add(employee);
}
return Ok(employees);
}
}
Definitely there are other options, such as using O/R mapping framework:
http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/NHibernateQueryableSample/.
http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/ODataDollarFormatSample/, which uses Entity Framework.

Hibernate: Adding a new element to association list does not persist

I have a ManyToMany association between two Entities: COURSE and STUDENT. They are associated through a STUDENT_COURSE_MAP association table.
Course.java:
#Entity
#Table(name="COURSE")
public class Course implements java.io.Serializable {
#ManyToMany(
mappedBy="courses",
cascade={CascadeType.PERSIST, CascadeType.MERGE}
)
private List<Student> students = Collections.emptyList();
// Rest of the class, getters, setters etc
}
Student.java:
#Entity
#Table(name="STUDENT")
public class Student implements java.io.Serializable {
#ManyToMany(
targetEntity=Course.class,
cascade = { CascadeType.PERSIST, CascadeType.MERGE }
)
#JoinTable(
name="STUDENT_COURSE_MAP",
joinColumns=#JoinColumn(name="STUDENT_REF"),
inverseJoinColumns=#JoinColumn(name="COURSE_REF")
)
private Set<Course> courses = Collections.emptySet();
// Rest of the class, getters, setters etc
}
I want to add a student (id=11) list for one particular course (id=77) as follows (transaction is already begun):
// Create new list and add a student to it
Student student_11 = (Student) session.get(Student.class.getName(), 11);
List<Student> studentsList = new ArrayList<Student>(1);
studentsList.add(student_11);
// Now, set the list of students in the course
Course course_77 = (Course) session.get(Course.class.getName(), 77);
course_77.setStudents(studentsList);
I save it using the following code:
session.flush();
session.getTransaction().commit();
However, the association - course_77 and student_11 - does not get persisted to the database. There are no insert statements generated in the hibernate log.
I even tried calling session.saveOrUpdate(course_77). Only the following is logged when saveOrUpdate is called:
allowing proxied method [saveOrUpdate] to proceed to real session
persistent instance of: org.whatra.tradecog.db.entity.Artist
ignoring persistent instance
object already associated with session: [Course#77]
Hints/tips/solutions are much appreciated.
Note 1:
The whole persistence thing works just fine for me when I create new Course and Student objects and save. What does not work is when I retrieve existing Course and existing Student objects and add a new association between them.
Here's what was wrong. It turns out I was saving only one side of the association. This is what I should have done:
// Create new list and add a student to it
Student student_11 = (Student)session.get(Student.class.getName(), 11);
Course course_77 = (Course) session.get(Course.class.getName(), 77);
List studentsList = new ArrayList(1);
studentsList.add(student_11);
Set coursesList = new HashSet(1);
coursesList.add(course_77);
course_77.setStudents(studentsList);
// THIS IS WHAT'S WRONG - I MISSED THIS NEXT LINE
student_11.setCourses(coursesList);
I found the answer at http://en.wikibooks.org/wiki/Java_Persistence/Relationships (look under 'Common Problems').

which collection is there to have a values of datamember of wcf if we don't know service contract?

which collection is there to have a values of datamember of wcf if we don't know service contract??
suppose my service contract is like
[ServiceContract]
[OperationContract]
Employee Getinfo(int id);
in the class where i implement it
Employee emp = new Employee();
emp.name ="abc";
emp.address = "pune";
return emp;
At Client side i have created object of Employee again like
ServiceReference1.Employee empobj = new ServiceReference1.Employee();
empobj = servicereferenceobject.Getinfo(3);
so empobj is giving me all data of employee.
BUT NOW I WANT SUCH COLLECTION THAT WORKS LIKE AN empobj IS THERE ANY???