Thanks for your help Vivek.
Database Tables and rows:
'CREATE TABLE `device` (
`device_id` int(11) NOT NULL AUTO_INCREMENT,
`device_name` varchar(45) NOT NULL,
`device_type_id` int(11) NOT NULL
PRIMARY KEY (`device_id`),
UNIQUE KEY `iddevice_UNIQUE` (`device_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8'
INSERT INTO `mydb`.`device` (`device_id`, `device_name`, `device_type_id`) VALUES ('1', 'Test 1', '1');
INSERT INTO `mydb`.`device` (`device_id`, `device_name`, `device_type_id`) VALUES ('2', 'Test 2', '1');
CREATE TABLE `device_type` (
`device_type_id` int(11) NOT NULL AUTO_INCREMENT,
`type_name` varchar(45) NOT NULL,
`subtype_name` varchar(45) NOT NULL,
PRIMARY KEY (`device_type_id`),
UNIQUE KEY `iddevice_type_UNIQUE` (`device_type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
INSERT INTO `mydb`.`device_type` (`device_type_id`, `type_name`, `subtype_name`) VALUES ('1', 'Test Type', 'Test Subtype');
Entities:
@Entity
@Table(name = "device", schema = "mydb")
public class Device {
@Id
@Column(name = "device_id")
private Integer id;
@Column(name = "device_name")
private String name;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "device_type_id")
private DeviceType type;
public Integer getId() {
return id;
}
public void setId(Integer id) {
}
public String getName() {
return name;
}
public void setName(String name) {
}
@Override
public String toString() {
return "Device {" +
"id = " + id + "," +
"name = " + name + "," +
"type = " + type + "," +
"}";
}
}
@Entity
@Table(name = "device_type", schema = "mydb")
public class DeviceType {
@Id
@Column(name = "device_type_id")
private int id;
@Column(name = "type_name")
private String type;
@Column(name = "subtype_name")
private String subtype;
public int getId() {
return id;
}
public void setId(int id) {
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSubtype() {
return subtype;
}
public void setSubtype(String subtype) {
this.subtype = subtype;
}
@Override
public String toString() {
return "DeviceType {" +
"id = " + id + "," +
"type = " + type + "," +
"subtype = " + subtype +
"}";
}
}
Persistence:
version="2.0">
<persistence-unit name="maria_pu">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<class>dev.model.Device</class>
<class>dev.model.DeviceType</class>
<properties>
<property name="kundera.client.lookup.class" value="com.impetus.client.rdbms.RDBMSClientFactory" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="org.mariadb.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/mydb" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.current_session_context_class" value="thread"/>
<!--<property name="hibernate.connection.password" value="impetus" />-->
<!-- If using hibernate 3, use this:
<property name="hibernate.current_session_context_class" value="org.hibernate.context.ThreadLocalSessionContext" />-->
<!--<property name="hibernate.current_session_context_class" value="org.hibernate.context.internal.ThreadLocalSessionContext" />-->
</properties>
</persistence-unit>
<persistence-unit name="cassandra_pu">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<properties>
<property name="kundera.nodes" value="localhost"/>
<property name="kundera.port" value="9160"/>
<property name="kundera.keyspace" value="environmentkeyspace"/>
<property name="kundera.dialect" value="cassandra"/>
<property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.pelops.PelopsClientFactory" />-->
<property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.thrift.ThriftClientFactory" />
</properties>
</persistence-unit>
</persistence>
Code that causes the problem:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("maria_pu");
EntityManager em = emf.createEntityManager();
try {
Device device = em.find(Device.class, 1);
System.out.println(device);
} catch (Exception e) {
System.out.println("Something bad happened!");
e.printStackTrace();
} finally {
em.close();
emf.close();
}
Expected output:
Device {id = 1,name = Test 1,type = DeviceType {id = 1,type = Test Type,subtype = Test Subtype},}
Thanks,
MK