Siegfried Heintze

Ranch Hand
+ Follow
since Aug 11, 2000
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Siegfried Heintze

There used to be an edit button so I could correct typos in my newly created post (that I missed using the previous feature). I don't see it now. Is it gone forever or just hiding?

Thanks
siegfried
1 year ago
I have a lot of questions on designing and implementing many-to-many relationships. It seems logical to me that there would be some book I could buy that would cover these issues but I have not found one yet. Can someone recommend a tutorial that discusses more than one brand of database?

I'm asking these questions for several reasons. Since I'm in between gigs I'm starting a new entrepreneurial effort that might make me some money and (more importantly) look good on my resume and help me brush up my java/database skills. I'm in the process of selecting database vendors, cloud vendors and frameworks (JDBC, springboot or EJB or ?, hibernate-database-first or hibernate-code-first, IBatis etc...)

Here is one sample many to many implementation from Microsoft: Northwind and PUBS sample databases for MSSQL Server. The Pubs database contains an implementation of a authors to book-titles many-to-many relationship I have been playing with.
(Yes, these are microsoft samples, but I'm interested in discussing junction tables in the context of the popular databases including MySQL, Oracle, PostgresSQL and MS SQL Svr).

To simplify things a bit, lets assume that my junction table only needs to contain the foreign key for the book and the foreign key for the author.

(1) What are some good naming conventions for the tables? Do I call the the table author or authors? books or book? What do I call the junction table? book2Author? What about reverse engineering feature in hibernate? I just discovered that has some rules about capitalization but I have not found them yet.

(2) I tend to use the popular approach of auto-increment integers as the primary key (I typically call this field "ID") for my tables. Does using an auto increment field called ID make sense for a junction table? It seems superfluous to me (unless of course, this is a ternary relationship and the auto increment id field of the junction table is going to be a foreign key field in a fouth table).

(3) What should I define as the primary key of a junction table? I think I want the concatenation of the two foreign keys to be unique to preserve referential integrity. Is this true? If so, should I make the primary key consist of the two foreign keys? Yikes -- now I am creating an index structure exclusively for referential integrity that will only hinder performance -- correct? When would I ever use a junction table if I already knew both foreign keys? The only time I would use a junction table is if I know the author_id and want the (multiple) book_id or I know the book_id and want the (multiple) author_ids. Correct? So what is the recommendation? Do I create a primary key consisting of both foreign keys, an index of just the author_id and another index of just the book_id so that each time I insert a row into the junction table I have to update three index structures?

(4) What is the favorite approach to populating these tables with test data? Is there a vendor neutral approach to dealing with SQL insert statements and those pesky auto increment fields? MS SQL Server and Oracle have a feature to allow you to temporarily specify the (otherwise) auto increment id fields of books and authors where you can subsequently hard code these ID fields as insert statements into the junction table (for test data only, of course). Alternatively, they also have features for retrieving the newly generated ID field from the INSERT statement. I think MSSql has the @@IDENTITY feature for this and MySQL has the last_insert_id(). Is there vendor neutral way of doing this? Are there any java/JDBC/hibernate/IBatis features to help me write vendor neutral code?

(5) Now what about production code and transactions? Do I need a transaction for anyone one of the CRUD operations on table that is part of a many-to-many relationship?
(5a) How do I insert into the book table, fetch the newly created integer ID, insert into the author table, fetch the second newly created ID and insert the two new IDs into the junction table? Do I have to create a transaction for this? How do I fetch the newly created IDs? When populating the test data I can hard code the integer ids (maybe) but not in production.
(5b) How do I delete a book? Do I delete the entries in the junction table first and then the book table? Does this need a transaction? What about the cascading delete feature (ON DELETE CASCADE) in MySQL? Do I want to use it? Can JDBC or hibernate or Ibatis help me implement "ON DELETE CASCADE" in a vendor neutral way?
(5c) What about reading? Do I need a transaction to find all the authors of a given book?
(5d) If I am just updating a book, I believe I don't need a transaction (assuming I'm not adding any authors). Is this true? Maybe not! What if I'm trying to update a book that has a single author and someone else is simultaneously trying to delete that book using the cascading delete feature?


Thanks
Siegfried

There is a great example of Gradle/Hibernate/hbm2/SpringBoot Spring5 & Angular Video Tutorial with Free Accompanying Source code From PacktPub.com in chapter two except it does not work!

Doing "./gradlew hbm2java" after having configured mysql gave this error:




When I tried bumping the spring boot version from 2.0.0.RC1 to 2.1.0.RELEASE I got a slightly different error:


Can someone show me a working example? Preferably with gradle using the hibernate plugin, but maven will do.

Thanks
Siegfried
1 year ago

Gary W. Lucas wrote:
So,  the JVM  uses the following per object
8 bytes management (including object-synchronization overhead)



Wow! 8 bytes seems like a lot to me -- especially when it is rarely used. I often see this

So java programmers often create a separate object just for locking. So why does this synchronization feature require 64 bits? Can anyone elaborate on the memory layout of these 64 bits (assuming 32 bit java).
Why must all descendants of Object consume this extra 64 bits of memory when it is so common to create a dedicated lock object (as in my example)? This seems extremely inefficient to require all objects to have this locking capability.
There is probably something I don't understand about locking... Perhaps someone can help me.


So could we save memory with this code?


Is this any better or worse?


Thanks
Siegfried
3 years ago

According to this video YouTube video on Java Memory Mgt from IBM the statement consumes 128 bits (assuming you are running 32 bit java) or 224 bits for 64 bit java.

Is this really true? Are there not some additional data structures required so the garbage collector can find all these bits of memory and de-fragment them? So what does x actually contain? Does it contain a pointer to the Integer object and that pointer needs to change after the garbage collector de-fragments the heap? If so, how does the garbage collector know where the variable x is? X must be on a linked list of active variables so the garbage collector can update "x" to the new location of the Integer object after the heap has been de-fragmented ... correct?

If that is true, then storing an integer object in java is much worse than 128 bits or 224 bits for 64 bit java. How bad is it?

Alternatively, the JVM could implement x as an integer index into a large fixed array of pointers. That array cell would contain the address of the actual integer object containing 10. If so, then the garbage collector would just update this large fixed array (which requires more memory too!). In either case, we need more memory. How much more?

Does anyone know how the Oracle (and other popular JVMs) implement this?

Thanks
Siegfried
3 years ago
Thank you everyone. That was very helpful.

Siegfried
3 years ago
I'm confused about declaring function arguments as List<Integer> versus List<? extends Integer> verses  List<? super Number>. Aside from it being a bit cumbersome to initialize a List<Number>, they all seem to work fine.

(1) Is not List<Integer> completely redundant with List<? extends Integer> because whenever we specify a (non-primitive) type, we are liberty to use that type or a descendant of that type?
I think this is the Liskov substitution principle. So what is the advantage of List<? extends Integer>?

(2) And what about the difference between function parameters of List verses List<Object> verses List<?>? Are these all redundant too?

(3a) And lastly, is it possible to efficiently modify the elements of an existing list instead of creating and returning a new list? While I suppose you could call get and set for a function parameter of type List, I'm worried these would be very inefficient for large linked lists. C++, for example, has a special for-each-loop syntax so you receive a reference to the linked list element instead of a copy.

In other words:
(3b) What is Java doing when I type "for (Integer n: nums){ n = n + 1; }" and nums is a List<Integer>? Apparently it is making a deep copy because when I print out the original List in the main program (or in this case, test function), the original list is not modified. Is there a way to make this do a shallow copy so I modify the original and avoid creating a new list as the function value?

Here is my code that passes all 6 tests:
3 years ago
I'm going nuts trying to find a working example of using Cassandra with a java ORM like Spring.framework.data. I've tried over a half dozen examples and none work
This makes me wonder if there is something wrong with the Cassandra server I am using.

However, this example works in java and groovy. Here is the abbreviated groovy version:

This makes me think that I have the v3.9 cassandra server working correctly.

So here is one of the many examples that I found from google searching that I cannot make work:
https://github.com/pivotal-cf/cf-cassandra-spring-example-app

I've tried this with Cassandra  (versions 3.7 and 3.9 and 2.0.8) and java 1.8 on Windows 10.



As far as I can tell, everytime I run cassandra 2.0.8, or 3.7 or 3.9, the server is listening on 127.0.0.1 and the default port 9042 and all my clients are also trying to connect to port 9042 (the default).

I've also tried these examples and cannot make them work either:

https://github.com/valchkou/SpringFrameworkCassandraSample

https://github.com/smartcat-labs/spring-cassandra-showcase-application

https://github.com/hellojeya/Demo-SpringBoot-Cassandra

https://github.com/boneill42/spring-data-jpa-cassandra.git

https://examples.javacodegeeks.com/enterprise-java/spring/spring-data-cassandra-example/

And some others I cannot remember where I downloaded them from.

Can someone help me fix
https://github.com/pivotal-cf/cf-cassandra-spring-example-app or find an example that works (preferable an example that uses JUnit to demonstrate that it works)?

Thanks
siegfried


3 years ago
Thanks!
I tried those hamcrest matchers and they seem to work for contianers (like sets) of strings.

However, I'm confused about how hamcrest compares containers of containers like Set<Collection<? extends String>>;

In other languages like C++, I can specify the lambda function or to be used for comparing elements of a Set container (for example). I assume in java, that function is Comparable::CompareTo. However, when I look at the documentation, I don't see that classes like List, or Set or ArrayList implement such a function. So how does class Set determine if you are adding a new element that is a duplicate? How does the hamcrest matcher know if a set contains a certain element (in any order)?

I tried creating descendants of ArrayList and List, and String that implement Comparable and failed (perhaps this should be a topic of a different post).
Like I suggested previously, I am surprised there is not some library in the maven repository that implements this already.

Can anyone improve on this awful code I wrote? It works, but it (my function compareIteratorCollections) does not use generics and is not as type-safe as it could be. Can someone show me how to write compareIteratorCollections using generics?



Here is my miserable matcher function:
3 years ago
Please help me understand why this test fails. The first assertThat passes but the assertThat in the for each loop fails.
This is probably because assertThat is doing shallow compares (comparing references) instead of deep compares (comparing the lists element by element).

Perhaps this is because List and ArrayList do not implement Comparable? If so, is there some library available in the maven repository that does have containers (such as lists) that implement Comparable?
If not, why?

3 years ago
Sorry for the confusion.

By maven library I mean JAR files available from the maven repository (probably a bad choice of terms! sorry!)

I have a function called GetPermutations that returns a Set<Collection<String>>.

I want to write a JUnit test for this function.

To write a JUnit test, I need a matcher to compare the actual and expected values.

So I create and populate a variable called expected of type Set<Collection<String>>.

Now, how do I compare "executed" with the returned value from GetPermuations?

Hamcrest is a library available on maven that will allow me to compare Sets if the elements of the set implement Comparable.

Unfortunately, ArrayLists, Arrays Lists don't implement Comparable.

Do you know of any libraries, preferably on the maven repository site, that implement collections that implement Comparable?

I could implement my own as I started to do. Surely someone has already done this and I could use their implementation.

I hope this helps.
Thank you for the very prompt response.

Siegfried
3 years ago
I need to write a JUnit test for a function that produces



When I started to implement this class:



I said to myself: "This is nuts! Surely someone has already done this for Array, LinkedList, ArrayList and friends!

But after google searching, I could not find any such examples...

Can someone point me to a maven library that will help me compare containers containing containers and maybe point me to an example?

I've been using the Hamcrest to compare Sets of Strings. Perhaps all I need is an example of using Hamcrest to compare Sets of Lists of Strings? I'm looking at the hamcrest javadocs and I'm not seeing anything obviouse.

Thanks
Siegfried
3 years ago
Darn! Where is that edit button?

Regarding "Why do I think JMS is a good option"?  I assumed it was. Clearly AMPQ has some additional features.

Regarding that queue.io link: Wow! that is a lot of options! I like ActiveMQ because I can download and install and execute with Maven easily on my local PC.

   Can I have some help selecting one that something similar that has the kind of throttle I described above?

Thanks!

Siegfried
3 years ago
I like this link and the code works:
https://www.ivankrizsan.se/2016/02/22/running-an-activemq-broker-with-maven/

After google searching "ActiveMQ Throttle" I was surprised that normally this means throttling the producer (or even the TCP connection) and that is not what I want.

So let me restate my question:
Does ActiveMQ have a feature to throttle consumers so that only one message total is received and processed at a time? In the case of throttling for Microsoft's WCF/MSMQ, you need to specify the maximum concurrent instances, concurrent sessions, and concurrent calls (of the consumer) to be one. This is what I want in a java based application.

After reading this link, I think the answer is no:  http://activemq.2283324.n4.nabble.com/Consumer-throttling-td4706685.html

If this is true, can someone recommend a java/groovy friendly message queue API or framework that does implement this kind of throttling?
From more google searching, it does not look like RabbitMQ supports this either. Strange. This is a feature of the Microsoft's WCF/MSMQ application I'm trying to port to an OS neutral implementation and I'm surprised to be having difficulty finding this feature in the java world.

Thanks
Siegfried


3 years ago

I want to write a prototype/proof-of-concept application that implements a (cygwin) bash execution queue server (and client) with a throttle so only one queue entry (job) gets executed at a time.

I know that this is a common feature in build environments but I need some simple source code as a starting point for some fancier enhancements.

What do you recommend? JMS via Spring or JMS via EJB? Do you like JMS?

How do I implement that throttle? Is this a feature of JMS or the implementation?
I also need to be able to enumerate entries in the queue and cancel (remove) some entries before they start execution.
And I need to be able to pause the queue so it does not remove the next entry and start execution.

So there is ActiveMQ, RabbitMQ, AMQ and others... Which have the features I need?
This needs to run on Linux and Windows...
Having a queue that can survive reboots would be nice too.
That is all the requirements I can think of right now...

Thanks,
Siegfried
3 years ago