EJB Find element's parameter ManyToOne - database

I have two classes related by ManyToOne, I have the id of "Chantier" and I'd like to find all the Lots which have this chantier's id.
public class Chantier implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String titre_aff;
private String num_aff;
private String ville;
public class Lots implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
List<String> numero_lot;
List<String> designation;
#ManyToOne
private Chantier chantier;
I tried with this in my LotsREST but it is looking for Lots with this id and not chantier :
#GET
#Path("{id}")
#Produces(MediaType.APPLICATION_JSON)
public Lots getLotByChantier(#PathParam("id") String id) {
return faL.find(Long.parseLong(id));
}

I just created this function to return what i wanted
#GET
#Path("{id}")
#Produces(MediaType.APPLICATION_JSON)
public List<Lots> getLotByChantier(#PathParam("id") String id) {
List<Lots> listLots = faL.findAll();
for (Lots lot : listLots) {
if (!lot.getChantier().getId().equals(Long.parseLong(id))) {
listLots.remove(lot);
}
}
return listLots;
}

Related

React.js - get enum values from class in Spring Boot

I have a class:
#Data
#Entity
public class NewOrder {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "Id")
private int Id;
private Date createdAt = new Date();
private Type types;
public enum Type {
ONDELIVERY, INADVANCE
}
#ManyToMany
private List<Product> products = new ArrayList<>();
public void addProduct(Product product) {
this.products.add(product);
}
}
I want to show available enum values of Type in React form. How could I get them? Is it possible without creating a new class with its own controller?

How can I model a drone medication delivery system in a database structure?

I have thought extensively about it and came up with these 2 classes:
public class Drone {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#NotEmpty(message = "Serial Number is required")
#Size(max = 100)
private String serial_number;
private ModelType model;
#Max(500)
private int weight_limit;
#NotEmpty(message = "Battery capacity is required")
private BigDecimal battery_capacity;
private StateType state;
}
and
public class Medication {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Pattern(regexp = "^[a-zA-Z0-9_-]*$")
private String name;
private float weight;
#Pattern(regexp = "^[a-zA-Z0-9_]*$")
private String code;
#Column(nullable = true, length = 64)
private String image;
}
But I just can't figure out how to link them since each drone will be carrying medications. I still think I'm missing something. Any ideas?

Cannot Convert String to Timespamp inside REST

Hello:) i really hope i can find help here!
the Problem: Im using Spring JPA, have created a basic REST Service, i added my Database, and i can find data in the databaste via entitymanager.createQuery()
BUT when i try to search for a timestamp or a datetime, it gives me this error:
15:12 SELECT b FROM StationsMessung b WHERE b.AVNR=:AVNR AND
b.TXNR=:TXNR AND b.DBTM=:DATUM
java.lang.ClassCastException: java.lang.String cannot
be cast to java.util.Date
at org.hibernate.type.descriptor.java.JdbcTimestampTypeDescriptor.unwrap(JdbcTimestampTypeDescriptor.java:24)
at org.hibernate.type.descriptor.sql.TimestampTypeDescriptor$1.doBind(TimestampTypeDescriptor.java:48)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:74)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:53)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:628)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2001)
at
org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1915)
at org.hibernate.... (show balloon)
i know it says it cannot convert String to java.util.Date, but i dont know how to solve it.thanks for every helper!
/////////////////////////////////////////////////////////ENTITY CLASS
#Entity
#IdClass(StationsMessung.class)
#Table(name = "****", schema = "***")
public class StationsMessung implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "AVNR")
private int AVNR; ////
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "TXNR")
private int TXNR;
#GeneratedValue(strategy = GenerationType.AUTO)
#Temporal(TemporalType.TIMESTAMP)
#Type(type = "date")
#Column(name = "DBTM")
private Timestamp DBTM;
public StationsMessung(int AVNR, int TXNR, Timestamp DBTM) {
this.AVNR = AVNR;
this.TXNR = TXNR;
this.DBTM = DBTM;
}
public StationsMessung() {
}
public int getAvnr() {
return AVNR;
}
public void setAvnr(int AVNR) {
this.AVNR = AVNR;
}
public int getTxnr() {
return TXNR;
}
public void setTxnr(int TXNR) {
this.TXNR = TXNR;
}
public Timestamp getDBTM() {
return DBTM;
}
public void setDBTM(Timestamp DBTM) {
this.DBTM = DBTM;
}
///////////////////////////////////////////////////////////SERVICE CLASS
#Service
public class StationService {
#PersistenceContext
EntityManager entityManager;
public List<Station> getAllStationMessungen(int AVNR, int TXNR, Timestamp DATUM) {
return entityManager.createQuery("SELECT b FROM StationsMessung b WHERE b.AVNR=:AVNR AND b.TXNR=:TXNR AND b.DBTM=:DATUM")
.setParameter("TXNR",TXNR )
.setParameter("AVNR",AVNR )
.setParameter("DATUM", DATUM)
.getResultList();
}
////////////////////////////////////////////////////////////CONTROLER CLASS
#RestController
#RequestMapping("/station")
public class StationController {
#Autowired //This annotation allows Spring to resolve and inject
collaborating beans into your bean
StationService stationService; //service
#RequestMapping(value = "/allmessungen/{AVNR}/{TXNR}/{DATUM}", method =
RequestMethod.GET)
public List<Station> getAllStationMessungen(#PathVariable int AVNR, int
TXNR, Timestamp DATUM) {
return stationService.getAllStationMessungen( AVNR, TXNR, DATUM);
}
now when i search for data without the timestamp,it shows me data,it works.
when i use a timestamp or date it shows me the error above.
THE INPUT OF ME IS:
PARAM.1 AVNR: 716
PARAM.2 TXNR: 1339
PARAM.3 DBTM: 2014-01-04 05:30:00
(its this format yyyy-mm-dd-hh24:mi:ss)
thanks for every help:)

Room database with one-to-much relation

I have 2 Entities: Employee and Specialty.
Every Employee can have some Specialties.
POJOs are generated.
#Entity(indices = {#Index(value = {"f_name", "l_name", "birthday",
"avatarLink"}, unique = false)}, inheritSuperIndices = true)
public class Employee implements Serializable {
#PrimaryKey(autoGenerate = true)
private int employeeId;
private String f_name;
private String l_name;
private String birthday;
#Ignore
private int age;
private String avatarLink;
#Embedded
private List<Specialty> specialty;
private final static long serialVersionUID = -8824149947485321362L;
#Ignore
public Employee() {
}
public Employee(int employeeId, String f_name, String l_name, String
birthday, String avatarLink, List<Specialty> specialty) {
this.employeeId = employeeId;
this.f_name = f_name;
this.l_name = l_name;
this.birthday = birthday;
this.avatarLink = avatarLink;
this.specialty = specialty;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
And some more setters\getters...
And Specialty
#Entity
public class Specialty implements Serializable {
#PrimaryKey
private int specId;
private String specName;
private final static long serialVersionUID = 4288061416169200241L;
public Specialty(int specId, String specName) {
this.specId = specId;
this.specName = specName;
}
#Ignore
public Specialty() {
}
public int getSpecId() {
return specId;
}
And some more setters\getters...
I have this error:
error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
I check so many questions and read docs, but it not helps me. Thank you
you should add #Ignore to serialVersionUID fields.
In view of Room, it's just another column.
#Ignore
private final static long serialVersionUID = 4288061416169200241L;

Getting AssertionError while using ObjectifyService.register

I am in the middle of trying to refactor some of my data models, but I've run into a problem that I don't understand.
Originally I had a simple data model comprised of 3 entity classes, which looked something like this:
#Entity
public final class Teacher {
#Id
private Long id;
private String primarySubject;
public Teacher() {}
public Teacher(String primarySubject) {
this.primarySubject = primarySubject;
}
//getters & setters
}
#Entity
public final class Student {
#Id
private String username;
#Load
#Index
private Ref<Teacher> homeRoomTeacher;
public Student() {}
public Student(String username, Teacher teacher) {
this.username = username;
homeRoomTeacher = Ref.create(teacher);
}
//getters & setters
}
#Entity
public final class School {
#Id
private String name;
#Load
private Set<Ref<Teacher>> teachers;
#Load
private Set<Ref<Student>> students;
public School() {}
public School(String name) {
this.name = name;
}
//getters & setters
}
And this all worked fine.
But we decided that it would be more useful for us to embed the entities directly instead of Refs...
#Entity
#Embed
public final class Teacher {
#Id
private Long id;
private String primarySubject;
public Teacher() {}
public Teacher(String primarySubject) {
this.primarySubject = primarySubject;
}
//getters & setters
}
#Entity
#Embed
public final class Student {
#Id
private String username;
#Index
private Ref<Teacher> homeRoomTeacher;
public Student() {}
public Student(String username, Teacher teacher) {
this.username = username;
homeRoomTeacher = Ref.create(teacher);
}
//getters & setters
}
#Entity
public final class School {
#Id
private String name;
private Set<Teacher> teachers;
private Set<Student> students;
public School() {}
public School(String name) {
this.name = name;
}
//getters & setters
}
After making those changes, then all of our junit tests started to fail with an AssertionError during registration of the School class in our test setup methods which look like:
#Before
public void setUp() throws Exception {
helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
helper.setUp();
ObjectifyService.register(Teacher.class);
ObjectifyService.register(Student.class);
ObjectifyService.register(School.class);
// more setup
}
The AssertionError doesn't appear until the line that registers the School class, and according to the stack trace is being thrown from the method "com.googlecode.objectify.impl.translate.CreateContext.enterCollection" but I'm not certain how to go about fixing it.
Does anyone have any ideas?
I suspect the error you are getting is to do with the fact that you are trying to register a class which you have annotated with #Embed.
The objectify documentation clearly states that #Embed classes do not have to be registered - maybe this is the cause of the issue.
Also, I'm not 100% sure on this but I don't think you need #Id on an embedded class.
I would suggest you give the following changes a go:
#Embed
public final class Teacher {
#Id
private Long id;
private String primarySubject;
public Teacher() {}
public Teacher(String primarySubject) {
this.primarySubject = primarySubject;
}
//getters & setters
}
#Embed
public final class Student {
#Id
private String username;
#Index
private Ref<Teacher> homeRoomTeacher;
public Student() {}
public Student(String username, Teacher teacher) {
this.username = username;
homeRoomTeacher = Ref.create(teacher);
}
//getters & setters
}
#Before
public void setUp() throws Exception
{
helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
helper.setUp();
ObjectifyService.register(School.class);
// more setup
}
Hope this helps!

Resources