Spring Boot cannot successfully POST data to db (ORA-00942) - java

I'm new to Spring Boot. I've stucked in the problem of creating new data (POST) for a whole day.
The error shows that I did not connect successfully to the db (ORA-00942).
Due to some privacy issues, I cannot copy-paste my work through my device. Therefore, I'm going to try my best to describe my logic and type out part of my codes. Any ideas would be a huge help! Thanks a million!
I have five packages here, which are basically classified by
(1.)SpringBootApplication package, (2.)Repository package(3.)Service package (4.)Controller
package (5.)Entity package
Basically, I do not edit in (1.)SpringBootApplication package, it's
kept originally as default.
In my (5.)Entity package, I have written an Entity class that matches
my db. Let's say the class name of this Entity is called TitleOfMyDb. Here, Ive written the correspond #id properly, and I also write the setters and getters correctly.
In the (3.)Service package, there is an Interface and a class. The class
is basically the implementation of the interface. Now, this is the trimmed
implementation of the service class.
#Autowired
private ExRepository exRepository;
#Overrride
public TitleOfMyDb createExampleMethod(String a, String b){
TitleOfMyDb titleOfMyDb = new TitleOfMyDb();
titleOfMyDb .setA(a);
titleOfMyDb .setB(b);
return exRepository.save(titleOfMyDb) //save method is originated from Repository instinctively.
In my (4.)Controller package, i have a class which is ExController:
#Autowired
private ExService exService;
#GetMapping("/test")
public TitleOfMyDb createSomething
(#RequestParam(value="aa") String a, #RequestParam(value="bb") String b){
TitleOfMyDb object = exService.createExampleMethod(a, b)
return object;
}
In my (2.)Repository package, I do not add more codes there, it's kept originally as well. because it already has the instinctive method that allows me to save() my entity with the repository.
Afterwards, when I try to run my spring boot, it shows error of ORA-00942. Also, when I type http://localhost:8080/test through my browser, I can only see nothing but error. The error message was quite complex, sorry that I really cannot copy-paste it through my device. Basically, it does not connect to my db properly.
Any help or guide on my logic and thinking process is really appreciated. Thank you!!!

ORA-00942: table or view does not exist
"You tried to execute a SQL statement that references a table or view that either does not exist, that you do not have access to, or that belongs to another schema and you didn't reference the table by the schema name."
I'd see if the database table is correct and double check the SQL query.
Ref: https://www.techonthenet.com/oracle/errors/ora00942.php

Error mean you don't have permision or your table incorrect. Please check entity. Make sure table : TitleOfMyDb exist in database. If you don't specific table with anotation #table hibernate automatic pick your name entity TitleOfMyDb

Related

How to use a result object from Jpa repository save() later in the same method that invoked save()?

In my controller I invoke a method from a service that will invoke save() on my database, and return me the object that was saved. That class that I am saving has a auto_generated id, so when i save it to the database, i expect to be returned with the id set (and that is working fine). In the same controller i store a result of that save() in a variable, and I found out that it's id is not set. That is because save() will actually save i to the database once that transaction is completed (when i exit the controller method. My problem is that i want to use that result before i exit my controller in a different service. How can I force the service (and consenquetly the repositroy) to save it immidiatelly and return me the result.
The reason for using id of classA in classB , is because classB is a "conncetion" table between two tables in database, and I should update it only when certain conditions are met, but this is of the point. I have already tried saveAndFlush() method in repository, that this service is calling, but it doesn't help. My service is only doing calling a save(), or saveAndFlush() method and nothing else (so it can't be a problem in the service).
I have already tried #Transactional annotation with REQUIRES_NEW, but it isn't working.
#PostMapping("")
public ClassA createClassA(#RequestBody ClassA classA){
ClassA a = classAService.saveClassA (classA);
System.out.println("Id = " + a.getIdClassA());
classBService.saveClassB(new classB(a.getId()); //it will cause an exception if a.getId() returns 0
return classA;
}
System.out.println will print out Id=0, but should print out Id=(some number that database makes, and cannot be zero because database has AUTO INCREMENT)
I have already tested all other services, repositories, connections etc. I am just interested how to force a response to come immidatelly so it can be stored in a variable and used later in the method.
Well, thank you for the comments, #JBNizet and #Lebecca you were both right :). Indeed saveAndFlush() would reslove my problem if I had told my class that id will be generated in the database. Thats why the soultion is to put something like #GeneratedValue(strategy = GenerationType.IDENTITY), and to add saveAndFlush(). It worked after these two steps.

Map Cassandra Materialized View with DSE's Java API

I have a cassandra table with an associated materialized view.
The primary key is a single id of type uuid, and I have no sort key. Let's call it my_table_id. This table contains a related_id that I want to use to search.
Then I have a materialized view for that table defined as
PRIMARY KEY (related_id, my_table_id) WITH CLUSTERING ORDER BY (my_table_id ASC)
PS: I realise it's the wrong way to partition data in Cassandra, but unfortunately, this code was inherited.
I'm defining my table in my java code as:
#Table(table = "my_table")
public class MyTableType {
#PartitionKey
#Column("my_table_id")
#Codec(MyIdCassandraConverter.class)
CustomUUIDType myTableId;
#Column("related_id")
#Codec(MyRelatedIdCassandraConverter.class)
MyRelatedId relatedId;
(...)
}
Those two custom types are simply wrappers around UUIDs. Again, inherited.
My materialized view is defined as:
#MaterializedView(baseEntity = MyTableType.class, view = "my_table_by_related_id")
public class MyTableTypeByRelatedId {
#PartitionKey
#Column("related_id")
#Codec(MyRelatedIdCassandraConverter.class)
MyRelatedId relatedId;
#ClusteringColumn
#Column("my_table_id")
#Codec(MyIdCassandraConverter.class)
CustomUUIDType myTableId;
}
The code seems to be generated correctly, but when I start my Spring Boot application, I get:
Error:java: Cannot find base entity class
'mypackage.MyTableType' for view class
'mypackage.MyTableTypeByRelatedId'
Error:java: Error while parsing: Cannot find base entity class
'mypackage.MyTableType' for view class
'mypackage.MyTableTypeByRelatedId'
There's some code generation going on, so it seems to be something's not generated correctly, but I can't quite figure out what.
The only mildly useful documentation I find is here and here, but none seems to offer help.
What am I doing wrong?
Well, not much of an answer, because this was not much of a question, but someone having a similar problem might find it useful.
This specific problem was caused because I was looking at the wrong database, which of course didn't have the tables and views I created. I had a property override file I thought was set correctly, and that was my mistake.
have you tried to add keyspace value on your #MaterializedView annotation?
#MaterializedView(keyspace = "xxxx", baseEntity = MyTableType.class, view = "my_table_by_related_id")

JPA EntityManager no longer finding entities, even when reverted to code that used to work

I am creating a server with Glassfish, Jersey, and a MySQL database
My EntityManager used to work but now does not
Here's an example of a simple method that used to work but now does not
#GET
#Path("movie/{id}")
#JSONP
#Produces({"application/javascript", MediaType.APPLICATION_JSON})
public Movie getMovie(#PathParam("id") int id) {
EntityManager em = (EntityManager) context.getAttribute("em");
Movie requestedMovie = em.find(Movie.class, id);
return requestedMovie;
}
It now returns java.lang.IllegalArgumentException: Unknown Entity bean class: class data.entry.Movie, please verify that this class has been marked with the #Entity annotation.
Bizarrely, it still does this even when I use git to roll my code back to a commit where I know it used to work.
Additionally, the code still works on my teammate's laptops.
The issue started when I attempted to add annotations to another class, and attempted to test it. This error started appearing. I then rolled my code back to previous commits to undo my changes, but I am still getting the error.
I have tried rebooting my laptop multiple times as well to no avail.
Any idea what could cause entity manager to stop being able to find entities?
Please try to remove built file.
It's mostly related to IDE cache / build cache.
Unknown Entity bean class: class data.entry.Movie, please verify that this class has been marked with the #Entity annotation.
And please check this too.
Solved the issue by uninstalling/reinstalling glassfish

Passing complex JPA Entities to the controller with POJO

My team is coding an application that involves editing wikipedia-like pages.
It is similar to the problem we have with registration:
A straightforward implementation gives something like
public static void doRegistration(User user) {
//...
}
The user parameter is a JPA Entity. The User model looks something like this:
#Entity
public class User extends Model {
//some other irrelevant fields
#OneToMany(cascade = CascadeType.ALL)
public Collection<Query> queries;
#OneToMany(cascade = CascadeType.ALL)
public Collection<Activity> activities;
//...
I have read here and there that this fails. Now, in Play!, what is the best course of action we can take? There must be some way to put all that data that has to go to the server in one object, that easily can be saved into the database.
EDIT: The reason this fails is because of the validation fail. It somehow says "incorrect value" when validating collection objects. I was wondering if this can be avoided.
SOLUTION: Changing the Collection to List has resolved the issue. This is a bug that will be fixed in play 1.2 :)
Thanks beforehand
this works. To be more clear, you can define a controller method like the one you wrote:
public static void doRegistration(User user) {
//...
}
That's completely fine. Just map it to a POST route and use a #{form} to submit the object, like:
#{form id:'myId', action:#Application.doRegistration()}
#{field user.id}
[...]
#{/form}
This works. You may have problems if you don't add all the field of the entity in the form (if some fields are not editable, either use hidden inputs or a NoBinding annotation as described here).
EDIT: on the OneToMany subject, the relation will be managed by the "Many" side. That side has to keep the id of the related entity as a hidden field (with a value of object.user.id). This will solve all related issues.
It doesn't fail. If you have a running transaction, the whole thing will be persisted. Just note that transactions are usually running within services, not controllers, so you should pass it from the controller to the service.

Hibernate gets null for entity instance variables?

I'm trying to implement a feature requires the form to load data for the logged in user.So I wrote a query to get these data for this user ID,Here is a code snippet from the school entity class:
public class ShSchool implements java.io.Serializable {
private long schoolId;
private GsDistrict gsDistrict;
}
I tried to get the data using the following query:
session.createQuery("from ShSchool where schoolId="+schoolId).list();
The problem is that I got values for primitive instance variables and got null for any other data types such as GsDistrict, So what is wrong and how could I got these objects values?
Thanks
Possible causes:
The gsDistrict is indeed null for that particular user, check the DB to make sure it's not.
The Hibernate mappings are incorrect.
The gsDistrict is lazy loaded and you're accessing it outside the Hibernate session. In this case, however, I'd expect an exception to be thrown.
Please include the Hibernate mappings, the problem may be there.

Categories