Is there any way to specify table prefixes in ActiveJDBC models? - activejdbc

Is there any way to change the table name of the models, or specify a table prefix, so that the model named People, for example, would reference the table TP_PEOPLE?

There is a way, you can use the #Table annotation, such as:
#Table("TP_PEOPLE")
public class People extends Model {}
however, I would suggest to call your class Person, since the instance of this class represent a single row from your table:
#Table("TP_PEOPLE")
public class Person extends Model {}
so that your code will look:
List<Person> people = Person.where("ssn = ?", ssn);

Related

Best way to model one to many relationship in OOP

I'm working in a project which involves a one to many relation in the database.
A simple example of this would be a teacher that teaches many courses but a course can be taught by just one teacher.
My question is what would be the best way to model this?
The first object is Teacher:
class Teacher{
public int id;
public String name;
public String lastName;
...
}
The thing is how would the course class look like?
Option 1:
class Course{
public int idCourse;
public String courseDescription;
**public int teacherId;**
...
}
Option 2:
class Course{
public int idCourse;
public String courseDescription;
**public Teacher teacher;**
...
}
Option 2 (having a reference to the teacher object) seems to be more functional from an OOP perspective.
In a usual flow, you would create a course object like :
Teacher t = new Teacher('first_name', 'last_name');
Course c = new Course('Math course');
c.setTeacher(t);
Your option 1 highlights what the relational database schema behind this might look like. Here's a good read on that :
https://www.lifewire.com/one-to-many-relationships-1019756#:~:text=A%20teacher%20can%20teach%20multiple,one%20teacher%20to%20multiple%20courses.
Once the code above is committed, it could fire two insert queries underlying to add the new teacher in teachers table and add the new course (along with the teacher ID), in the course table.

Kotlin: Scoping using Object vs Class

I have a few data classes, that is short, so I group them together in a file.
I can defined them in a Kotlin file as it is, but would prefer it to be scope within a class/object, so the file is not just a file, but under class/object for better grouping
I could do
object Model {
data class Result(val query: Query)
data class Query(val searchinfo: SearchInfo)
data class SearchInfo(val totalhits: Int)
}
and I could also do
class Model {
data class Result(val query: Query)
data class Query(val searchinfo: SearchInfo)
data class SearchInfo(val totalhits: Int)
}
They both looks the same to me. What's the different, and if there's a preferred way in term of scoping my data classes?
I would advise against using classes for scoping other classes. As Todd explains in his answer, you can use sealed classes which offer an actual benefit of exhaustive when checks. If you don't need this feature, Kotlin has a built-in mechanism for scoping - packages:
package org.company.model
data class Result(val query: Query)
data class Query(val searchinfo: SearchInfo)
data class SearchInfo(val totalhits: Int)
I can defined them in a Kotlin file as it is, but would prefer it to be scope within a class/object, so the file is not just a file, but under class/object for better grouping
There's nothing wrong with a file containing multiple top-level elements. This is a useful language feature and is used in exactly this kind of situation.
Another option is to make all of your data classes a subclass of a sealed Model class. This will give you the benefit of defining them all in one place, because Kotlin enforces that for sealed classs. Also, having the type system know about all instances of type Model is helpful in when expressions as well because then it won't require you to put an else -> block.
sealed class Model
data class Result(val query: Query) : Model()
data class Query(val searchinfo: SearchInfo) : Model()
data class SearchInfo(val totalhits: Int) : Model()
And you can just use them directly:
val r = Result(Query(SearchInfo(3))
Instead of wrapping them in another class or object, where you'd have to refer to them like this:
val r = Model.Result(Model.Query(Model.SearchInfo(3)))

how does EBean map classes to tables?

How does EBean map classes to tables?
I have tables like "expense_details" and classes like model.ExpenseDetails, for example, and the classes just use the #Entity annotation without explicit mapping to a table.
Is the mapping done automatically by EBean (is it smart enough with all those "_" and CamelCase names)?
Yes it is, it uses exactly rule that you described, for SomeModel model it looks for some_model table, of course you can override it ie. using #Table annotation like:
#Entity
#Table(name = "my_custom_table_name")
public class SomeModel extends Model {
...
}

Inheritence Strategy when some entities do not have any specific columns or table

In database we have a "parent" table with a discriminator column, and depending on this discriminator column some columns are stored in specific tables or some relationships (many/one-to-many) are allowed.
In our mapping we wanted to implement it like
#Entity
#Inheritance(strategy = InheritanceType.JOINED)
public class A {
#Id
#Column
private int id;
}
#Entity
public class B extends A {
#Column
private String specificField;
}
#Entity
public class C extends A {
#OneToMany
private List<OtherEntity> otherEntities;
}
Unfortunately Hibernate wants to join on table C, which does not exist since it would only contain the FK to A. Is there a way to keep this strategy but tell that no join is necessary?
Otherwise, what is the cleanest solution? Following this question I thought using the SINGLE_TABLE strategy with the #SecondaryTable annotation on child entities that require it but it seems heavier to configure (having to declare, for each column, the source table).

NHibernate Inheritance Mapping

I know in NHibernate you can have inheritance mappings, and I know you can have table-per-class, table-per-subclass and table-per-concrete-class but they don't quite fit the scenario I have.
Basically what I want is to be able to have a base class called product that looks like this:
public class BaseProduct
{
public virtual int ProductId {get;set;}
public virtual string ProductName {get;set;}
}
which maps directly to a product table.
Then I want to have a Product class which inherits from BaseProduct like this:
public class Product : BaseProduct
{
public virtual IList<Category> Categories {get;set;}
}
The thing is that the Product class should still map to the product table, the only difference being that this implementation has a list of Categories attached.
Without going into the technical reasons for why I need to do this, I would like to know if it's at all possible?
From your question and comment, I get that you want «Single Table Inheritance» [PoEAA, Fowler], but don't have the luxury of being able to add the needed discriminator to the table.
I've never run into this situation myself, but try to add a discriminator to your mapping which is a calculated value/derived field that uses sql to find out if there are foreign keys from Category (won't work for Products with empty Category collections though).
If your scenario is a read-only one, and you can add views to the DB, it's an option to map to a Product view with the discriminator calculated as stated above.
You are looking for a table perclass you need to set a discriminator-value
NHDoc for inheritance
Do you have other classes that inherit from BaseProduct? If not, you can just map the Product class only.